【问题标题】:Find and Replace a string in a text file and output to another file在文本文件中查找和替换字符串并输出到另一个文件
【发布时间】:2018-10-14 22:14:31
【问题描述】:

我正在尝试编写一个程序,它可以打开一个文本文件,找到某个字符串并将其替换为另一个字符串,然后将更改后的文本写入输出文件。

这是我迄今为止编写的代码。它工作正常,除了输出文件缺少空格和换行符。

我需要保留所有空格和换行符。我该怎么做?

#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;
    }

}

【问题讨论】:

  • 从流中提取标记默认会跳过空格。您需要手动添加它们。此外,如果您不重新打开流,则调用close() 是多余的。 std::fstream 的析构函数为你做这些
  • 就 close() 而言,我们的教授一直在做。至于添加空格,如果它不仅跳过空格,而且还有新行,我该如何手动添加它们。我怎么知道是否添加空格换行符?
  • 换行符也是空格。我正在研究下面的答案。应该在 5 分钟内准备好
  • 如果我们不使用close(),她会直接扣分。
  • 似乎 Sam 先发布了他的答案。这是一个很好的。至于你的老师——你可能想冷静地告诉她,RAII 已经成为C++ 的一部分 20 多年了,也许她想升级她的知识以匹配上一个千年末的知识。只是说

标签: c++ file visual-c++ io


【解决方案1】:
while (filein >> temp)

这个temp 变量是一个std::string。格式化提取运算符&gt;&gt; 重载std::string 会跳过输入中的所有空白字符(空格、制表符、换行符)并完全丢弃它们。这个格式化的提取运算符会丢弃所有空格,直到第一个非空格字符,然后提取它和所有后续的非空格字符并将它们放入您的std::string,也就是这个temp 变量。这就是它的工作原理。

随后:

fileout << temp;

然后将这个字符串写到输出中。显示的代码中没有任何内容告诉您的计算机按原样将所有空格从输入复制到输出。显示的代码唯一要做的就是从输入文件中提取每个非空格字符序列,立即将所有空格和换行符扔在地板上,再也看不到了;然后将剩下的内容(进行适当的更改)写入输出文件。并且计算机总是会完全按照您的指示去做,而不是按照您的意愿去做。

while (filein >> temp)

这是输入文件中的所有空格都被扔进垃圾箱并被丢弃的地方。因此,您希望保留它们并将它们复制到输出文件中,您将不得不替换它。

这里有几种方法可以使用。最简单的解决方案是一次简单地读取输入文件一个字符。如果它不是空白字符,请将其添加到 temp 缓冲区。如果它是一个空格字符,并且temp 不为空,那么你只是读了一个完整的单词;检查是否需要更换;将其写入输出文件;清除temp 缓冲区(准备阅读下一个单词);然后手动将刚刚读取的空白字符写入输出文件。以这种方式,您将输入复制到输出,一次一个字符,包括空格,但将非空格字符缓冲到temp 缓冲区,直到读取每个完整的单词,然后再将其复制到输出文件。而且您还需要处理处理文件中最后一个单词的边缘情况,没有任何尾随空格。

【讨论】:

  • 您并没有完全实现我写下的内容。仔细阅读我的答案,并将其与您自己的代码进行比较。试着找出你的新代码的哪一部分正确地实现了我在第 2 段的第 4 句中写下的所有内容。你找不到它,因为你没有实现它。
猜你喜欢
  • 1970-01-01
  • 2017-03-05
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2020-03-25
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多