簡體   English   中英

查找並替換文本文件中的字符串,然后輸出到另一個文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM