C++中ifstream读取数据写入ofstream失败问题排查
我正在做一个项目,需要打开文件、处理数据、识别特定特征并筛选对应行。程序运行时控制台输出的结果是正确的,但尝试把这些数据写入指定的ofstream文件时,文件里完全没有内容。以下是我的代码:
#include <fstream> #include <iomanip> #include <iostream> #include <vector> #include <cmath> using namespace std; int main() { string fileName; ifstream iFile; iFile.open(fileName); // keep asking the user for file name again and again till the file opens successfully while(1){ cout<<"Open File: "; cin>>fileName; iFile.open(fileName);//open file if(!iFile){ cout << "Could not open " << fileName << endl; }else//valid file name,exit the loop break; } string plate; string date; string time; string weight; string speed; int foundWeight = 0; int foundSpeed = 0; int count = 0; vector<string> v1; vector<string> v2; ofstream outFile("[10-09-2022] Report.txt:", ios::app); while (getline(iFile, plate, ',')) { getline(iFile, date, ',') ; getline(iFile, time, ',') ; getline(iFile, weight, ','); foundWeight = stoi(weight); getline(iFile, speed); foundSpeed = stoi(speed); if(foundWeight < 5000 && foundSpeed > 45){ cout << "[" << time << "]" << ' ' << plate << endl; outFile << "[" << time << "]" << ' ' << plate << endl; count++; }else if(foundWeight >= 5000 && foundSpeed > 30){ cout << "[" << time << "]" << ' ' << plate << endl; outFile << "[" << time << "]" << ' ' << plate << endl; count++; } } cout << count << " violations logged.\n"; outFile << count; outFile.close(); return 0; }
程序运行输出:
Open File: a5_short.dat
[06:11] MVE-717
[06:23] OLC-651
[06:28] KQJ-359
[06:37] NFT-062
[06:41] MCW-399
[06:45] BWQ-255
[06:49] ZKL-544
[07:00] FHQ-348
[07:11] QME-845
[07:13] OWG-325
[07:34] NNJ-571
[07:43] ZKI-228
12 violations logged.
显示的数值都是正确的,但就是无法写入到"[10-09-2022] Report.txt:"文件里。
修复方案
修正输出文件名
输出文件名末尾的冒号:是非法字符(Windows等系统不允许文件名包含冒号),导致文件无法创建或写入。将文件名改为"[10-09-2022] Report.txt"。检查输出文件是否成功打开
和输入文件的处理逻辑一致,添加对outFile的状态检查,避免因权限、路径错误等问题导致写入失败。移除多余的输入文件打开操作
代码开头的iFile.open(fileName);执行时fileName是空字符串,会破坏输入流的状态,导致后续读取异常,直接删除这行代码。补充完整的文件输出内容
控制台输出了完整的统计信息,但文件只写入了数字count,建议保持输出内容一致,提升报告可读性。
修复后的完整代码:
#include <fstream> #include <iomanip> #include <iostream> #include <vector> #include <cmath> using namespace std; int main() { string fileName; ifstream iFile; // 循环请求文件名直到成功打开 while(true){ cout << "Open File: "; cin >> fileName; iFile.open(fileName); if(!iFile){ cout << "Could not open " << fileName << endl; }else{ break; } } string plate; string date; string time; string weight; string speed; int foundWeight = 0; int foundSpeed = 0; int count = 0; vector<string> v1; vector<string> v2; // 修正输出文件名,去掉非法的冒号 ofstream outFile("[10-09-2022] Report.txt", ios::app); // 检查输出文件是否成功打开 if(!outFile){ cout << "Could not open output file!" << endl; return 1; } while (getline(iFile, plate, ',')) { getline(iFile, date, ',') ; getline(iFile, time, ',') ; getline(iFile, weight, ','); foundWeight = stoi(weight); getline(iFile, speed); foundSpeed = stoi(speed); if(foundWeight < 5000 && foundSpeed > 45){ cout << "[" << time << "]" << ' ' << plate << endl; outFile << "[" << time << "]" << ' ' << plate << endl; count++; }else if(foundWeight >= 5000 && foundSpeed > 30){ cout << "[" << time << "]" << ' ' << plate << endl; outFile << "[" << time << "]" << ' ' << plate << endl; count++; } } string logMsg = to_string(count) + " violations logged."; cout << logMsg << endl; outFile << logMsg << endl; // 关闭文件流 outFile.close(); iFile.close(); return 0; }
内容的提问来源于stack exchange,提问作者annon101

