使用c+替换文本文件中的单词+; 我正在寻找一种方法,用C++来替换我的HTML文件中的某个字符串(不是整行)。例如,如果我有一个包含以下内容的html文件: </span><br><span class=text>morning<br></span></td> 早晨

使用c+替换文本文件中的单词+; 我正在寻找一种方法,用C++来替换我的HTML文件中的某个字符串(不是整行)。例如,如果我有一个包含以下内容的html文件: </span><br><span class=text>morning<br></span></td> 早晨,c++,C++,我想把它编辑成: </span><br><span class=text>night<br></span></td> 夜间 我需要将“早上”替换为“晚上”,这是我的代码: string strReplace = "morning"; string strNew = "night"; ifstream filein("old_file.html"); ofstream fileout("new_file.

我想把它编辑成:

 </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;
   }
string strReplace=“晨”;
字符串strNew=“夜间”;
ifstream文件IN(“old_ufile.html”);
流文件输出(“new_file.html”);
字符串strTemp;
while(filein>>strTemp)
{
如果(strTemp==strReplace){
strTemp=strNew;
}
strTemp+=“\n”;

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

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");

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

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;
}
接下来,执行
查找和替换
功能:

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;
std::string contents=getfile(filein);
查找和替换(内容为“早”、“晚”);

fileout有多种处理方法。一种非常常见的方法是使用新字符串创建文件,删除旧文件,然后将新文件重命名为旧文件名

如果这对您合适,您可以拦截这个控制台应用程序


参数1是要搜索的字符串

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

参数3是文件路径


基本上,我正在向新文件生成一些STD流,并从旧文件中查找有问题的字符串(如果存在),如果存在,则替换该字符串,然后通过管道传递结果,不管替换是否作为新文件的新行发生

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

如果退出代码aka errorlevel为2,则表示输入流不工作。如果为-1,则表示我们没有成功地在tact中找到并替换为新文件。如果为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;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
如果(argc!=4){返回1;}
int exit_code=-1;
string textToFind=string(argv[1]);
string replacementText=string(argv[2]);
string fileToParse=string(argv[3]);
字符串fileToWrite=fileToParse+“.rep”;
ifstream inputFileStream(fileToParse,ifstream::in);
如果(!inputFileStream.good()){return 2;}
流输出文件流(fileToWrite);
字符串文件行;
while(getline(inputFileStream,fileLine))
{
size\t substringPos=fileLine.find(textToFind);
if(substringPos!=字符串::npos)
{
fileLine.replace(substringPos,textToFind.size(),replacementText);
}       

outputFileStream filein>>读取令牌。令牌由空格清除。请尝试
class=text>morning
。尝试使用逐行读取文件,然后使用查找子字符串,如果找到子字符串,请替换为。非常感谢您提供的详细答案!