【问题标题】:Why is my program giving error (file handling)?为什么我的程序会出错(文件处理)?
【发布时间】:2020-01-06 16:36:07
【问题描述】:
#include<iostream>
#include<cstring>
#include<fstream>

using namespace std;

ofstream fileData;
ifstream writeData;

void newAccount();


int main()
{
    string data[10][3];
    int choice=0,i,j;


    fileData.open("D:\\Desktop\\input.txt");

    for(i=1; i<10&&(!fileData.eof());i++)
    {
        for(j=0; i<3;j++)
            getline(fileData,data[i][j]);
    }

    fileData.close();

    for(i=0; i<10;i++)
    {
        for(j=0; i<3;j++)
            cout<<data[i][j];
    cout<<endl;
    }

    while(choice!=8)
    {

        cout<<"             Hi, Welcome to the Bank"
            <<"\n\n1.New Account.\n2.Deposit Money\n3.Withdraw Money\n4.Balance Enquiry"
            <<"\n5.Account Holder List\n6.Close An Account\n7.Modify An Account\n8.Exit\n\n";

        cin>>choice;

        if(choice==1)
        {
            newAccount();
        }

    }

它的说法

main.cpp: In function ‘int main()’:
main.cpp:38:40: error: no matching function for call to ‘getline(std::ofstream&, std::string&)’
             getline(fileData,data[i][j]);

【问题讨论】:

标签: c++ file-handling


【解决方案1】:

您正在尝试从std::ofstream 读取,这是一个输出流。使用std::ifstreamstd::fstream

【讨论】:

  • 现在它又给了我一个错误...它似乎无法找到该文件...我正在使用在线编译器...您可以推荐一些关于它的东西吗??
  • 对于在线编译器,您可能需要阅读文档以确定文件是否可行以及如何创建和使用。
  • D:\\Desktop\\input.txt 用于在线编译器?对于一条路径来说似乎很奇怪。我希望大多数在线编译器都作为 linux 容器运行。尽管即使这是一个 Windows 容器,D:\\Desktop 文件夹也会很奇怪。
  • 我是编程新手,所以我不太了解细节。我应该为相应的文件使用什么路径?我正在使用 GDB。
  • 尝试:"input.txt" 而不是 "D:\\Desktop\\input.txt",但是您的应用程序可能需要在尝试读取之前创建此文件。除非在线编译器允许您访问某些虚拟文件系统(首先阅读有关在线编译器的文档)。这假设在线编译器完全允许文件。由于额外的工作+存储需求和安全隐患,可能无法启用允许文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多