繁体   English   中英

使用 C++ 替换文本文件中的单词

[英]Replace a word in text file using c++

我正在寻找一种使用 C++ 为我的 html 文件替换某个字符串(不是整行)的方法。 例如,如果我有一个包含以下内容的 html 文件:

</span><br><span class=text>morning<br></span></td>

我希望它编辑为:

 </span><br><span class=text>night<br></span></td>

我需要用“晚上”替换“早上”,这是我的代码:

  string strReplace = "morning";
  string strNew = "night";
  ifstream filein("old_file.html");
  ofstream fileout("new_file.html");
  string strTemp;
  while(filein >> strTemp)
  {
    if(strTemp == strReplace){
       strTemp = strNew;
    }
    strTemp += "\n";
    fileout << strTemp;
   }

这段代码对我的文件没有任何影响,我猜原因是它只能更改整行,而不是部分字符串。 有人可以给我一些建议来正确实施吗? 提前谢谢你。

从我阅读原始问题的方式来看,您需要用“晚上”替换文件中所有“早上”的实例,而不仅仅是给定行上的一个实例。 我首先将整个文件读入一个字符串。

std::string getfile(std::ifstream& is) {
  std::string contents;
  // Here is one way to read the whole file
  for (char ch; is.get(ch); contents.push_back(ch)) {}
  return contents;
}

接下来,创建一个find_and_replace函数:

void find_and_replace(std::string& file_contents, 
    const std::string& morn, const std::string& night) {
  // This searches the file for the first occurence of the morn string.
  auto pos = file_contents.find(morn);
  while (pos != std::string::npos) {
    file_contents.replace(pos, morn.length(), night);
    // Continue searching from here.
    pos = file_contents.find(morn, pos);
  }
}

然后在主要,

std::string contents = getfile(filein);
find_and_replace(contents, "morning", "night");
fileout << contents;

编辑: find_and_replace()不应将其file_contents字符串参数声明为const 刚刚注意到并解决了这个问题。

您可以取整行,然后搜索并替换子字符串。 这样,您甚至可以跳过整个循环:

std::string line;

//Get line in 'filein'
std::getline(filein, line);

std::string replace = "morning";

//Find 'replace'
std::size_t pos = line.find(replace);

//If it found 'replace', replace it with "night"
if (pos != std::string::npos)
    line.replace(pos, replace.length(), "night");

有多种方法可以处理它。 一个很常见的做法是创建一个包含新字符串的文件,删除旧文件,然后将新文件重命名为旧文件名。

如果这适合您,您可以使用此控制台应用程序。


参数 1 是要搜索的字符串

参数 2 是要使用的替换文本

参数3为文件路径


基本上,我正在将一些 STD 流发送到新文件,并从旧文件中找到有问题的字符串是否存在,如果存在,则替换该字符串,然后通过管道传输结果,无论替换是否作为新文件发生新文件的行。

然后,我们关闭流,检查文件大小,如果成功创建一个非空文件,我们将删除旧文件并将新文件重命名为旧文件名。

如果退出代码又名 errorlevel 为 2,则表示输入流不起作用。 如果它是 -1,则意味着我们没有成功找到并替换为完整的新文件。 如果为 1,则表示您没有提供正确的参数。 只有零退出将表示成功。

如果你想使用它,你应该添加一些异常处理,并且可能是一个更强大的文件备份解决方案。

#include <iostream>
#include <fstream>
#include <string>
#include <sys\stat.h>

using namespace std;

int main(int argc, char * argv[])
{
    if (argc != 4) { return 1; }
    int exit_code = -1;

    string textToFind = string(argv[1]);
    string replacementText = string(argv[2]);
    string fileToParse = string(argv[3]);
    string fileToWrite = fileToParse+".rep";

    ifstream inputFileStream(fileToParse, ifstream::in);
    if (!inputFileStream.good()) { return 2; }

    ofstream outputFileStream(fileToWrite);

    string fileLine;
    while (getline(inputFileStream, fileLine))
    {
        size_t substringPos = fileLine.find(textToFind);
        if (substringPos != string::npos)
        {
            fileLine.replace(substringPos, textToFind.size(), replacementText);
        }       
        outputFileStream << fileLine << '\n';
    }
    outputFileStream.close();
    inputFileStream.close();

    struct stat st;
    stat(fileToWrite.c_str(), &st);
    int new_file_size = st.st_size;

    if (new_file_size > 0)
    {
        if (remove(fileToParse.c_str())==0)
        {
            if (rename(fileToWrite.c_str(), fileToParse.c_str())==0)
            {
                exit_code = 0;
            }
        }
    }

    return exit_code;
}

暂无
暂无

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

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