程序不会读取文件中的第一个条目

Program won't read past the first entry in file

我需要帮助修复此 C++ 程序,该程序未将一个文件中的第二个条目写入另一个文件。它似乎只将第一个条目写入文件,然后 while 循环终止,即使尚未到达文件末尾。这是程序:

indata.open("income.dat", ios::out | ios::binary);

while (true)
{
    indata.ignore();
    indata.getline(person[count2].name, NAMESIZE); 
    indata >> person[count2].income;
    indata >> person[count2].rent;
    indata >> person[count2].food;
    indata >> person[count2].utilities;
    indata >> person[count2].miscell;

    if (count2 == 0)
    {
        outdata << setw(20) << "Name"
                << setw(10) << "Income"
                << setw(10) << "Rent" 
                << setw(10) << "Food"
                << setw(15) << "Utilities"
                << setw(15) << "Miscellaneous"
                << setw(10) << "Net Money" << endl << endl;
    }

    outdata << setw(20) << person[count2].name 
            << setw(10) << person[count2].income 
            << setw(10) << person[count2].rent 
            << setw(10) << person[count2].food 
            << setw(15) << person[count2].utilities 
            << setw(15) << person[count2].miscell 
            << setw(10) << person[count2].net << endl;

    count2++;
}

outdata.close();

我在 while 循环中测试的条件包括 indata!indata.eof()indata.good()

有什么想法吗?谢谢

我解决了这个问题:

对于这种特殊情况,我无法弄清楚如何让 while 循环迭代多次,所以我使用了 for 循环:

indata.open("income.dat", ios::out | ios::binary);

   for (count2 = 0 ; count2 < count + 1 ; count2++)
   {
    indata.ignore();
    indata.getline(person[count2].name, '\n'); 
    indata >> person[count2].income;
    indata >> person[count2].rent;
    indata >> person[count2].food;
    indata >> person[count2].utilities;
    indata >> person[count2].miscell;
    indata >> person[count2].net;

    if (count2 == 0)
    {   
        // write information to output file
        outdata << setw(20) << "Name" << setw(10) << "Income" << setw(10) << "Rent" 
            << setw(10) << "Food" << setw(15) << "Utilities" << setw(15)
                << "Miscellaneous" << setw(10) << "Net Money" << endl << endl;
    }

    outdata << setw(20) << person[count2].name 
                << setw(10) << person[count2].income 
                << setw(10) << person[count2].rent 
                << setw(10) << person[count2].food 
                << setw(15) << person[count2].utilities 
                << setw(15) << person[count2].miscell 
                << setw(10) << person[count2].net << endl;
   }

   outdata.close();

对于这种特殊情况,使用前一个实例的计数作为 for 循环的条件。