C++读取文本文件时陷入第一行循环的问题求助
文件读取死循环重复第一行的问题解决
问题描述
作业需求:从文本文件读取数据,根据字符拆分到不同输出文件。示例输入数据:
5555 t 2345 2345 f 9910 5520 x 1850
遇到的问题:代码处理完第一行后无法读取下一行,持续循环第一行内容。尝试移除.eof()判断、删除条件逻辑后问题依然存在,怀疑和char类型的使用有关。
原代码
#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; int main() { ifstream Infile; ofstream OutfileTree; ofstream OutfileFlow; ofstream ErrorFile; int saleNum, price, flowerTotal=0, treeTotal=0; int current_line =0; char itemType; Infile.open("TreeIn.txt"); OutfileTree.open("TreesOut.txt"); OutfileFlow.open("FlowersOut.txt"); ErrorFile.open("Errors.txt"); if (!Infile) { cout <<"Cannot open Infile, terminating program" << endl; exit (1); } if (!OutfileTree) { cout <<"Cannot open Outfile Tree, terminating program" << endl; exit (1); } if (!OutfileFlow) { cout <<"Cannot open Outfile Flow, terminating program" << endl; exit (1); } if (!ErrorFile) { cout <<"Cannot open ErrorFile, terminating program" << endl; exit (1); } OutfileTree << fixed << showpoint << setprecision(2); OutfileFlow << fixed << showpoint << setprecision(2); while (!Infile.eof()) { Infile >> saleNum >> itemType >> price; if (itemType =='T') OutfileTree << "Sales Number:" << saleNum << " " << "Price:" << price << endl; else if (itemType =='F') { OutfileTree << "Sales Number:" << saleNum << " " << "Price:" << price << endl; } else { ErrorFile << "Error" << endl; ErrorFile << saleNum << price << endl; } } Infile.close(); OutfileTree.close(); OutfileFlow.close(); ErrorFile.close(); }
问题根源
- 大小写不匹配触发流错误:示例数据中的类型字符是小写
t/f,但代码判断的是大写'T'/'F',第一次读取后进入else分支。若后续读取时流因格式错误进入失败状态,所有后续读取都会失效,变量保留第一次的数值,导致死循环。 .eof()的误用:while (!Infile.eof())会在读取到文件末尾后仍执行一次循环,复用上一次的变量值。但核心问题还是流错误状态未被处理。- 输出逻辑错误:处理
'F'类型时错误输出到OutfileTree而非OutfileFlow,属于业务逻辑偏差。
修正后的代码
#include <iostream> #include <iomanip> #include <fstream> #include <string> #include <cctype> using namespace std; int main() { ifstream Infile; ofstream OutfileTree; ofstream OutfileFlow; ofstream ErrorFile; int saleNum, price, flowerTotal=0, treeTotal=0; char itemType; Infile.open("TreeIn.txt"); OutfileTree.open("TreesOut.txt"); OutfileFlow.open("FlowersOut.txt"); ErrorFile.open("Errors.txt"); if (!Infile) { cout << "无法打开输入文件,程序终止" << endl; exit(1); } if (!OutfileTree) { cout << "无法打开树木输出文件,程序终止" << endl; exit(1); } if (!OutfileFlow) { cout << "无法打开花卉输出文件,程序终止" << endl; exit(1); } if (!ErrorFile) { cout << "无法打开错误文件,程序终止" << endl; exit(1); } OutfileTree << fixed << showpoint << setprecision(2); OutfileFlow << fixed << showpoint << setprecision(2); // 将读取操作放入循环条件,读取成功才进入循环 while (Infile >> saleNum >> itemType >> price) { // 统一转换为大写,兼容大小写输入 char upperType = toupper(itemType); if (upperType == 'T') { OutfileTree << "Sales Number:" << saleNum << " " << "Price:" << price << endl; treeTotal += price; } else if (upperType == 'F') { // 修正输出目标文件 OutfileFlow << "Sales Number:" << saleNum << " " << "Price:" << price << endl; flowerTotal += price; } else { // 输出完整错误行信息 ErrorFile << "错误行:" << saleNum << " " << itemType << " " << price << endl; } } // 输出总计信息(可选) OutfileTree << "Total: " << treeTotal << endl; OutfileFlow << "Total: " << flowerTotal << endl; Infile.close(); OutfileTree.close(); OutfileFlow.close(); ErrorFile.close(); return 0; }
关键修正说明
- 循环条件优化:
while (Infile >> saleNum >> itemType >> price)直接利用流读取的返回值判断,读取成功(流状态正常)才进入循环,避免.eof()的延迟问题,同时读取失败时直接终止循环。 - 大小写兼容:通过
toupper()将输入字符转换为大写,不管输入是小写还是大写都能正确匹配,解决了示例数据的匹配问题。 - 输出逻辑修正:将
'F'类型的输出目标改为OutfileFlow,符合业务需求。 - 错误信息优化:输出完整的错误行内容,便于排查问题。
内容的提问来源于stack exchange,提问作者Metally
相关产品推荐
相关产品推荐

