C++文本文件操作:如何修改代码实现FX替换为TL、BQ分块输出
问题描述
现有inputFile.txt内容如下:
Start FX FX FX FX End
需要将文件中的FX分别替换为TL与BQ,要求两类替换结果的行数与原文件FX数量完全一致(各4行),且按全部TL行在前、全部BQ行在后的顺序输出到outputFile.txt,期望输出内容为:
Start TL TL TL TL BQ BQ BQ BQ End
原有代码实现得到TL与BQ逐行交替的错误结果,实际输出为:
Start TL BQ TL BQ TL BQ TL BQ End
原有实现代码:
void replaceInFile(string inputFile, string outFile) { string toBeReplaced = "FX"; string toReplaceWith1 = "TL"; string toReplaceWith2 = "BQ"; ifstream inputStream(inputFile); ofstream outputStream(outFile); if (!inputStream.is_open() || !outputStream.is_open()) { cerr << "Either open input or output file failed!"; } string line; string duplicateLine; size_t len = toBeReplaced.length(); while (getline(inputStream, line)) { duplicateLine = line; for (size_t pos = line.find(toBeReplaced); pos != string::npos; pos = line.find(toBeReplaced, pos)) { if (pos) { line.replace(pos, toBeReplaced.length(), toReplaceWith1); duplicateLine.replace(pos, toBeReplaced.length(), toReplaceWith2); outputStream << duplicateLine << endl; } } outputStream << line << endl; } inputStream.close(); outputStream.close(); }
核心问题
原有代码采用边读边输出的流式逻辑,每处理一行FX就立刻输出两个替换行,直接导致TL与BQ逐行交替,无法实现“先输出全部TL、再输出全部BQ”的要求。同时存在文件打开失败无返回、FX在行首时替换遗漏的冗余判断问题。
修改后代码
#include <fstream> #include <iostream> #include <string> #include <vector> using namespace std; void replaceInFile(string inputFile, string outFile) { const string toBeReplaced = "FX"; const string tlReplace = "TL"; const string bqReplace = "BQ"; ifstream inputStream(inputFile); ofstream outputStream(outFile); if (!inputStream.is_open() || !outputStream.is_open()) { cerr << "Either open input or output file failed!"; return; // 补充打开失败后的返回,避免异常执行 } // 一次性读取所有输入行,避免流式输出的交替问题 vector<string> allLines; string line; while (getline(inputStream, line)) { allLines.push_back(line); } // 分类存储各类行 vector<string> headLines; // FX行之前的非FX行(如Start) vector<string> tailLines; // FX行之后的非FX行(如End) vector<string> tlLines; // 所有FX行替换为TL的结果 vector<string> bqLines; // 所有FX行替换为BQ的结果 bool fxEncountered = false; for (const string& originalLine : allLines) { const size_t fxPos = originalLine.find(toBeReplaced); if (fxPos == string::npos) { // 无FX的行按位置归入头部或尾部 if (!fxEncountered) { headLines.push_back(originalLine); } else { tailLines.push_back(originalLine); } } else { fxEncountered = true; // 生成TL替换行,保留原行其他格式 string tlLine = originalLine; tlLine.replace(fxPos, toBeReplaced.length(), tlReplace); tlLines.push_back(tlLine); // 生成BQ替换行 string bqLine = originalLine; bqLine.replace(fxPos, toBeReplaced.length(), bqReplace); bqLines.push_back(bqLine); } } // 按要求顺序输出 for (const string& l : headLines) outputStream << l << endl; for (const string& l : tlLines) outputStream << l << endl; for (const string& l : bqLines) outputStream << l << endl; for (const string& l : tailLines) outputStream << l << endl; inputStream.close(); outputStream.close(); }
修改逻辑说明
- 先完整读取所有输入行,按行属性分类存储,避免逐行输出的交替问题。
- 替换时仅修改FX字段,完全保留原行的空格、格式,匹配需求的输出样式。
- 输出顺序严格按照「头部非FX行 -> 全部TL替换行 -> 全部BQ替换行 -> 尾部非FX行」排列,符合要求。
内容的提问来源于stack exchange,提问作者Tee
相关产品推荐
相关产品推荐

