英文:
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<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();
}
}
its saying
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]);
答案1
得分: 2
使用std::ofstream
是一个输出流,你正试图从中读取。请使用std::ifstream
或std::fstream
。
英文:
You are trying to read from std::ofstream
which is an output stream. Use std::ifstream
or std::fstream
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论