我的程序为什么出现错误(文件处理)?

huangapple go评论72阅读模式
英文:

Why is my program giving error (file handling)?

问题

#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; j < 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]);
英文:
#include&lt;iostream&gt;
#include&lt;cstring&gt;
#include&lt;fstream&gt;

using namespace std;

ofstream fileData;
ifstream writeData;

void newAccount();


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

    fileData.open(&quot;D:\\Desktop\\input.txt&quot;);
    
    for(i=1; i&lt;10&amp;&amp;(!fileData.eof());i++)
    {
        for(j=0; i&lt;3;j++)
            getline(fileData,data[i][j]);
    }
    
    fileData.close();
    
    for(i=0; i&lt;10;i++)
    {
        for(j=0; i&lt;3;j++)
            cout&lt;&lt;data[i][j];
    cout&lt;&lt;endl;
    }
    
    while(choice!=8)
    {
    
        cout&lt;&lt;&quot;             Hi, Welcome to the Bank&quot;
            &lt;&lt;&quot;\n\n1.New Account.\n2.Deposit Money\n3.Withdraw Money\n4.Balance Enquiry&quot;
            &lt;&lt;&quot;\n5.Account Holder List\n6.Close An Account\n7.Modify An Account\n8.Exit\n\n&quot;;

        cin&gt;&gt;choice;
    
        if(choice==1)
        {
            newAccount();
        }
    
    }

its saying

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

答案1

得分: 2

使用std::ofstream是一个输出流,你正试图从中读取。请使用std::ifstreamstd::fstream

英文:

You are trying to read from std::ofstream which is an output stream. Use std::ifstream or std::fstream.

huangapple
  • 本文由 发表于 2020年1月7日 00:36:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615756.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定