如何在C++中操作文本文件

How to manipulate text file in C++

我有一个包含以下数据的 txt 文件 (inputFile.txt):

Start
 FX 
 FX
 FX
 FX
End

我想要实现的是将 FX 替换为 TLBQ 这样我既重复了4次同样的FX次数。见下文(预期结果 - outputFile.txt):

Start
 TL 
 TL 
 TL
 TL 
 BQ
 BQ
 BQ
 BQ
End

然而,在我当前的实施中,我得到了以下结果(当前结果):

Start
 TL
 BQ
 TL 
 BQ 
 TL
 BQ
 TL
 BQ
End

下面是我当前的代码:

void replaceInFile(string inputFile, string outFile)
{
    string toBeReplaced = "FX";

    string toReplaceWith1 = "TL";
    string toReplaceWith2 = "BQ";

    ifstream inputStream(inputFile);
    ofstream outputStream(outFile);

    if (!inputStream.is_open() || !outputStream.is_open())
    {
        cerr << "Either open input or output file failed!";
    }

    string line;
    string duplicateLine;

    size_t len = toBeReplaced.length();
    while (getline(inputStream, line))
    {
        duplicateLine = line;

        for (size_t pos = line.find(toBeReplaced); pos != string::npos; pos = line.find(toBeReplaced, pos))
        {
            if (pos)
            {
                line.replace(pos, toBeReplaced.length(), toReplaceWith1);
                duplicateLine.replace(pos, toBeReplaced.length(), toReplaceWith2);

                // This line creates the duplicate
                outputStream << duplicateLine << endl;
            }
        }
        outputStream << line << endl;
    }

    inputStream.close();
    outputStream.close();
}

如何修改上面的代码以获得预期的结果/outputFile.txt?

以下是我如何编辑您的代码,使其按照我认为的方式工作:

string line;
string duplicateLine;
std::vector<string> toBeReplacedLines1;
std::vector<string> toBeReplacedLines2;

size_t len = toBeReplaced.length();
while (getline(inputStream, line))
{
    if (line == "Start" || line == "End")
        continue;
    duplicateLine = line;
    for (size_t pos = line.find(toBeReplaced); pos != string::npos; pos = line.find(toBeReplaced, pos))
    {
        if (pos)
        {
            line.replace(pos, toBeReplaced.length(), toReplaceWith1);
            duplicateLine.replace(pos, toBeReplaced.length(), toReplaceWith2);
        }
    }
    toBeReplacedLines1.push_back(line);
    toBeReplacedLines2.push_back(duplicateLine);
}
outputStream << "Start\n";
for (int i = 0; i < toBeReplacedLines1.size(); i++)
{
    outputStream << toBeReplacedLines1[i] << endl;

}
for (int i = 0; i < toBeReplacedLines2.size(); i++)
{
    outputStream << toBeReplacedLines2[i] << endl;
}
outputStream << "End";
inputStream.close();
outputStream.close();

当然,我介绍了vector的用法,大家不要忘记包含

我的建议:

// Do not forget const reference
void replaceInFile( const string& inputFile, const string& outFile)
{
    ifstream inputStream(inputFile);
    ofstream outputStream(outFile);

    if (!inputStream.is_open() || !outputStream.is_open())
    {
        cerr << "Either open input or output file failed!";
        
       // Do not forget to exit your fonction
       return;
    }

    // 1. First create the targeted string:
    string targetedString = "";
    for( int i = 0; i < 3; i++){
       targetedString.append("TL\n" );}

    for( int i = 0; i < 3; i++){
       targetedString.append("BQ\n" );}


    // 2. Then browse your file
    string line = "";
    while (getline(inputStream, line))
    {
        // Case where you need to replace the "FX" string
        if( line.find( "FX" ) != string::npos)
           outputStream << targetedString << endl;
    
        // Otherwise you do no transformation 
        else
           outputStream << line << endl;
    }

    inputStream.close();
    outputStream.close();
}