[英]Find and Replace a string in a text file and output to another file
我正在尝试编写一个程序,该程序可以打开文本文件,找到某个字符串,然后用另一个字符串替换它,然后将更改后的文本写入输出文件。
到目前为止,这就是我编写的代码。 它工作正常,除了输出文件缺少空格和换行符。
我需要保留所有空格和换行符。 我该怎么做?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string search = "HELLO"; //String to find
string replace = "GOODBYE"; //String that will replace the string we find
string filename = ""; //User-provided filename of the input file
string temp; //temp variable for our loop to hold the characters from the file stream
char c;
cout << "Input filename? ";
cin >> filename;
ifstream filein(filename); //File to read from
ofstream fileout("temp.txt"); //Temporary file
if (!fileout || !filein) //if either file is not available
{
cout << "Error opening " << filename << endl;
return 1;
}
while (filein >> temp) //While the stream continues
{
if (temp == search) //Check if the temp variable has captured the string we are looking for
{
temp = replace; //When we found the string, we substitute it with the replacement string
}
fileout << temp; //Dump everything to fileout (our temp.txt file)
}
//Close our file streams
filein.close();
fileout.close();
return 0;
}
更新:
我按照您的建议进行了以下操作,但现在根本不起作用(以前的代码可以正常工作,但空格除外)。 您能告诉我我在做什么错吗? 谢谢。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string search = "or"; //String to find
string replace = "OROROR"; //String that will replace the string we find
string filename = ""; //User-provided filename of the input file
string temp = ""; //temp variable for our loop to hold the characters from the file stream
char buffer;
cout << "Input filename? ";
cin >> filename;
ifstream filein(filename); //File to read from
ofstream fileout("temp.txt"); //Temporary file
if (!fileout || !filein) //if either file is not available
{
cout << "Error opening " << filename << endl;
return 1;
}
while (filein.get(buffer)) //While the stream continues
{
if (buffer == ' ') //check if space
{
if (temp == search) //if matches pattern,
{
temp = replace; //replace with replace string
}
}
temp = string() + buffer;
for (int i = 0; temp.c_str()[i] != '\0'; i++)
{
fileout.put(temp.c_str()[i]);
}
return 0;
}
}
while (filein >> temp)
这个temp
变量是std::string
。 std::string
的格式化提取运算符>>
重载会跳过输入中的所有空白字符(空格,制表符,换行符),并将其完全丢弃。 此格式化的提取运算符将丢弃所有空格,直到第一个非空格字符,然后将其以及所有后续的非空格字符提取出来,并将其放入此temp
变量std::string
。 这就是它的工作方式。
随后:
fileout << temp;
然后,将此字符串写到输出中。 显示的代码中没有任何内容告诉您的计算机按原样将所有空白从输入复制到输出。 所示代码唯一要做的就是从输入文件中提取每个非空格字符序列,立即将所有空格和换行符扔到地板上,再也看不到了。 然后将剩下的内容(进行适当的更改)写入输出文件。 而且计算机将始终完全按照您的指示执行操作,而不是您要执行的操作。
while (filein >> temp)
这是输入文件中的所有空格都扔到垃圾箱中并丢弃的地方。 因此,您希望保留它们并将它们复制到输出文件中,就必须替换它。
这里可以使用几种方法。 最简单的解决方案是一次只读取输入文件一个字符。 如果不是空格字符,请将其添加到temp
缓冲区。 如果它是一个空白字符,并且temp
不为空,那么您已经读了一个完整的单词; 检查是否需要更换; 将其写到输出文件中; 清除temp
缓冲区(以准备读取下一个单词); 然后手动将刚读取的空白字符写入输出文件。 通过这种方式,您将一次将输入复制到输出中,每个字符一个字符(包括空格),但是将非空格字符缓冲到temp
缓冲区中,直到每个完整的单词都被读取为止,然后再将其复制到输出文件中。 而且,您还需要处理文件中最后一个单词的边缘情况,而没有任何尾随空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.