我正在做一项家庭作业,我们要从文件中读取公司数据,然后对其进行处理以查找错误。
到目前为止,我认为第一行可以使用,但我不确定如何让它读取之后的每一行。每一行都是一个包含 ID、姓名和付款的记录。基本上我想知道在处理完第一行之后如何跳到下一行。我还没有包括错误检查,但我认为它将在读取 1 条记录后的最后一个 do while 循环中。如果读入每个变量的信息是错误的,我可以检查它并将其输出到摘要文件或错误文件。
void processFile()
{
string filename;
ifstream recordFile;
ofstream summary("summary.dat");
ofstream error("error.dat");
cout << "Please enter a filename\n";
do
{
cin >> filename;
recordFile.open(filename);
if (!recordFile.is_open())
cout << "No file by that name. Please enter another filename\n";
}
while(!recordFile.is_open());
int ID = 0;
string firstName;
string lastName;
double payment1, payment2, payment3 = (0.00, 0.00, 0.00);
string numberOfRecords;
getline(recordFile, numberOfRecords);
do
{
ws(recordFile);
recordFile >> ID;
recordFile >> firstName;
recordFile >> lastName;
recordFile >> payment1;
recordFile >> payment2;
recordFile >> payment3;
}
while(!recordFile.eof());
}
*编辑:我发现了我的问题的一部分,我实际上需要跳过第一行并从那一点开始阅读。每个文件的第一行包含无用的数据。