c++ - 从文件中读取和填充单独的变量

标签 c++ file visual-c++ file-io file-processing

我正在做一项家庭作业,我们要从文件中读取公司数据,然后处理错误。

到目前为止,我认为第一行可以使用,但我不确定如何让它读取每一行。每行都是一条包含 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());
}

*edit :我发现了部分问题,实际上我需要跳过第一行并从那一点继续阅读。每个文件的第一行都有无用的数据。

最佳答案

ifstream 对象上使用getline 函数

关于c++ - 从文件中读取和填充单独的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12804429/

相关文章:

c++ - 如何将opencv mat图像转换为gdi位图

c++ - 由于 while 循环中的条件而导致段错误

c - 当文本文件为空时如何停止读取字符串

c++ - freopen() 的 Python 版本

visual-c++ - _CrtDumpMemoryLeaks( ) == 1 在第一行代码上?

c++ - 函数 `feof` 总是返回 1

c - 将姓名和号码插入链表末尾

c++ - 调试时 MFC 断言失败

visual-c++ - 如何在XP(C++)中获取公共(public)桌面文件夹路径?

C++ std::cout 缓冲区重定向段错误