请求编写C++文本处理函数:按规则处理文件行并输出至新文件
代码问题排查与文本处理逻辑修正
需求回顾
编写一个函数,接收输入文件路径字符串指针与输出文件路径字符串指针,实现以下逻辑:对输入文本的每一行,若该行单词数为偶数,则删除每隔一个单词(保留第1、3、5...个单词);若单词数为奇数,则保持该行不变,最终将结果写入输出文件。
示例输入输出
Input.txt
I do not like to go to school or study I like to play gamesOutput.txt
I not to to or I like to play games
现有代码
#include <iostream> #include <fstream> #include <string> #include <algorithm> using namespace std; int main() { string in, out; cin >> in; cin >> out; ifstream input(in); string str; ofstream output(out); string st; while (getline(input, str)) { do { int i = count(str.cbegin(), str.cend(), ' '); if (i % 2 == 0) st.append(str); else st.append(" "); i++; } while (input); output << st << endl; } return 0; }
核心问题分析
- 单词数计算逻辑错误:你用空格数量判断奇偶,但单词数是空格数+1。比如示例第一行有9个空格,对应10个单词(偶数),但你的代码会把空格数9当成奇数,错误执行保留原行的逻辑,完全违背需求。
- 内层do-while循环完全无效:这个循环反复计算当前行的空格数,不断把整行或空格追加到
st里,直到文件流失效,会导致每行被重复处理无数次,输出内容彻底混乱。 - 未实现核心筛选逻辑:代码完全没有拆分每行的单词,也没有做“保留每隔一个单词”的筛选操作,根本没触及需求的核心步骤。
- 变量未及时清空:
st会累积所有行的处理结果,即使逻辑正确,每行输出的也是之前所有行的内容总和。 - 潜在编译错误:使用
std::count需要包含<algorithm>头文件,你的代码如果没加的话会编译失败。
修正后的代码(非函数版本)
#include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector> #include <algorithm> using namespace std; int main() { string inPath, outPath; cin >> inPath >> outPath; ifstream input(inPath); ofstream output(outPath); // 检查文件是否成功打开 if (!input.is_open() || !output.is_open()) { cerr << "无法打开目标文件" << endl; return 1; } string line; while (getline(input, line)) { istringstream lineStream(line); vector<string> words; string word; // 将整行拆分为单个单词 while (lineStream >> word) { words.push_back(word); } string processedLine; int wordCount = words.size(); if (wordCount % 2 == 0) { // 单词数为偶数,保留第1、3、5...个单词(对应索引0、2、4...) for (int i = 0; i < wordCount; i += 2) { if (!processedLine.empty()) { processedLine += " "; } processedLine += words[i]; } } else { // 单词数为奇数,直接保留原行 processedLine = line; } // 将处理后的行写入输出文件 output << processedLine << endl; } // 关闭文件流 input.close(); output.close(); return 0; }
封装为函数的实现
按照需求将核心逻辑封装成函数,接收文件路径指针:
#include <iostream> #include <fstream> #include <string> #include <sstream> #include <vector> #include <algorithm> using namespace std; // 处理文本文件的函数,接收输入输出路径指针,返回处理结果 bool processTextFile(const char* inputPath, const char* outputPath) { ifstream inputFile(inputPath); ofstream outputFile(outputPath); if (!inputFile.is_open() || !outputFile.is_open()) { return false; } string line; while (getline(inputFile, line)) { istringstream lineStream(line); vector<string> words; string word; while (lineStream >> word) { words.push_back(word); } string processedLine; int wordCount = words.size(); if (wordCount % 2 == 0) { for (int i = 0; i < wordCount; i += 2) { if (!processedLine.empty()) { processedLine += " "; } processedLine += words[i]; } } else { processedLine = line; } outputFile << processedLine << endl; } inputFile.close(); outputFile.close(); return true; } int main() { string inPath, outPath; cin >> inPath >> outPath; if (!processTextFile(inPath.c_str(), outPath.c_str())) { cerr << "文件处理失败,请检查路径是否正确" << endl; return 1; } return 0; }
内容的提问来源于stack exchange,提问作者Gently Barry
相关产品推荐
相关产品推荐

