简体   繁体   English

查找并替换文本文件中的字符串,然后输出到另一个文件

[英]Find and Replace a string in a text file and output to another file

I'm trying to write a program that can open a text file, find a certain string and substitute it with another string and then write the altered text to an output file. 我正在尝试编写一个程序,该程序可以打开文本文件,找到某个字符串,然后用另一个字符串替换它,然后将更改后的文本写入输出文件。

This is what I've coded so far. 到目前为止,这就是我编写的代码。 It works fine, except for that the output file is missing spaces and new line characters. 它工作正常,除了输出文件缺少空格和换行符。

I need to preserve all spaces and new line characters. 我需要保留所有空格和换行符。 How do I do it? 我该怎么做?

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

UPDATE: 更新:

I followed your advice and did the following, but now it doesn't work at all (the previous code worked fine, except for white spaces). 我按照您的建议进行了以下操作,但现在根本不起作用(以前的代码可以正常工作,但空格除外)。 Could you kindly tell me what I'm doing wrong here? 您能告诉我我在做什么错吗? Thank you. 谢谢。

#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)

This temp variable is a std::string . 这个temp变量是std::string The formatted extraction operator, >> , overload for a std::string skips all whitespace characters (spaces, tabs, newlines) in the input and completely discards them. std::string的格式化提取运算符>>重载会跳过输入中的所有空白字符(空格,制表符,换行符),并将其完全丢弃。 This formatted extraction operator discards all whitespace until the first non-whitespace character, then extracts it and all following non-whitespace characters and places them into your std::string , which is this temp variable. 此格式化的提取运算符将丢弃所有空格,直到第一个非空格字符,然后将其以及所有后续的非空格字符提取出来,并将其放入此temp变量std::string This is how it works. 这就是它的工作方式。

Subsequently: 随后:

fileout << temp;

This then writes out this string to the output. 然后,将此字符串写到输出中。 There's nothing in the shown code that tells your computer to copy all whitespace from the input to the output, as is. 显示的代码中没有任何内容告诉您的计算机按原样将所有空白从输入复制到输出。 The only thing that the shown code does is extract every sequence of non-space characters from the input file, immediately throwing on the floor all spaces and newlines, never to be seen again; 所示代码唯一要做的就是从输入文件中提取每个非空格字符序列,立即将所有空格和换行符扔到地板上,再也看不到了。 and then write what's left (with the appropriate changes) to the output file. 然后将剩下的内容(进行适当的更改)写入输出文件。 And a computer will always do exactly what you tell it to do, and not what you want it to do. 而且计算机将始终完全按照您的指示执行操作,而不是您要执行的操作。

while (filein >> temp)

This is where all spaces in the input file gets thrown in the trash, and discarded. 这是输入文件中的所有空格都扔到垃圾箱中并丢弃的地方。 Therefore you wish to preserve them and copy them to the output file, as is, you will have to replace this. 因此,您希望保留它们并将它们复制到输出文件中,就必须替换它。

There are several approaches that can be used here. 这里可以使用几种方法。 The simplest solution is to simply read the input file one character at a time. 最简单的解决方案是一次只读取输入文件一个字符。 If it's not a whitespace character, add it to the temp buffer. 如果不是空格字符,请将其添加到temp缓冲区。 If it's a whitespace character, and temp is not empty, then you've just read a complete word; 如果它是一个空白字符,并且temp不为空,那么您已经读了一个完整的单词; check if it needs replacing; 检查是否需要更换; write it out to the output file; 将其写到输出文件中; clear the temp buffer (in preparation for reading the next word); 清除temp缓冲区(以准备读取下一个单词); and then manually write the just-read whitespace character to the output file. 然后手动将刚读取的空白字符写入输出文件。 In this manner you will copy the input to the output, one character at a time, including spaces, but buffering non-space character into the temp buffer, until each complete word gets read, before copying it to the output file. 通过这种方式,您将一次将输入复制到输出中,每个字符一个字符(包括空格),但是将非空格字符缓冲到temp缓冲区中,直到每个完整的单词都被读取为止,然后再将其复制到输出文件中。 And you will also need to handle the edge case of handling the very last word in the file, without any trailing whitespace. 而且,您还需要处理文件中最后一个单词的边缘情况,而没有任何尾随空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM