程序不会读过文件中的第一个条目
Program won't read past the first entry in file
我需要帮助修复此C 程序,该程序没有将第二个文件中的第二个条目写入另一个文件。它似乎仅将第一个条目写入文件,然后在此之后终止时,即使未达到文件的末尾。这是程序:
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()
。
有什么想法吗?谢谢。
我解决了这个问题:
我无法弄清楚如何在此特定情况下迭代多次迭代,所以我使用了循环:
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循环的条件。
相关文章:
- 我有两个类需要在同一 cpp 文件中相互引用,但第一个类无法识别第二个类类型的对象
- 获取每个目录或子目录中第一个文件的路径
- 将内容从第一个文件("constituencies")移动到第二个文件("temp")并在之后重命名时,我的文件被删除
- CFileFind不考虑目录的第一个文件
- glob只给出匹配模式的第一个文件
- Makefile仅跟踪第一个文件
- 查找第一个文件W通配符匹配
- 查找第一个文件/查找下一个文件不返回文件夹中的所有文件
- 查找第一个文件问题无法使任何示例正常工作。
- 提取所有文件名 - 查找第一个文件
- 查找第一个文件在根路径上失败
- 如何制作初始文件的 10 个副本,如果第一个文件是 AS-1.txt第二个应该是 AS-2.txt依此类推
- Win32 查找第一个文件非终止通配符扩展
- LINUX/C++从第二个文件中删除第一个文件中的字符串
- 如何读取两个文件并正确关闭第一个文件?C++
- C 编程 - 查找第一个文件返回Invalid_handle_value
- C++后续文件在第一个文件之后无法打开
- 如何使用C++(VS)将n个第一个文件移动到另一个目录
- 查找第一个文件总是返回INVALID_HANDLE_TRUE
- Makefile指令只编译第一个文件