C++ 如何将一个文件复制到另一个文件中,但用用户输入的单词替换一个单词?

C++ 如何将一个文件复制到另一个文件中,但用用户输入的单词替换一个单词?,c++,C++,我试图将一个文件复制到另一个文件,但用用户输入的内容更改了一个单词。到目前为止,我已经得出了以下结论: while (getline(openningTheFile, line, ' ')) //line is a string and openningTheFile is an ifstream { if (line == wordToBeDeleted) { line = wordToReplaceWith; } if (line == "

我试图将一个文件复制到另一个文件,但用用户输入的内容更改了一个单词。到目前为止,我已经得出了以下结论:

while (getline(openningTheFile, line, ' ')) //line is a string and openningTheFile is an ifstream 
{
    if (line == wordToBeDeleted)
    {
        line = wordToReplaceWith;

    }
    if (line == "\n")
    {
        newFile << endl; //newFile is an ofstream 
    }

    newFile << line << " ";
}
while(getline(openningTheFile,line“”)//line是字符串,openningTheFile是ifstream
{
如果(行==要删除的字)
{
line=单词替换为;
}
如果(行==“\n”)
{
新建文件您正在使用
std::getline
读取一行文本

您需要在文本行中找到该单词,替换该单词,然后将文本行写入输出文件

一种方法是使用
std::stringstream
和操作符
>
从字符串中提取单词


另一种方法是使用
std::string::find
来定位单词的位置。

您可以通过修改循环从使用
std::istringstream读取的行中逐字读取来实现这一点:

while (getline(openningTheFile, line)) 
{ 
    std::istringstream iss(line);
    std::string word;
    bool firstWord = true;
    while(iss >> word)
    {
        if(word == wordToBeDeleted) {  
            newFile << wordToReplaceWith;
            if(!firstWord) {
                newFile << " ";
            }
            firstWord = false;
        }
    }
    newFile << `\n`;
}
while(getline(openningTheFile,line))
{ 
标准::istringstream iss(线);
字符串字;
bool firstWord=true;
while(iss>>word)
{
如果(word==wordToBeDeleted){
新文件我推荐的策略:

  • 使用
    std::getline
    逐行读取文件
  • 使用
    std::string::find
    在行中查找要替换的字符串
  • 如果找到该字符串,请将其替换为新字符串
  • 重复步骤2和3,直到找不到字符串
  • 输出更新的行
  • 以下是这方面的核心代码:

    while (getline(openningTheFile, line)) 
    { 
       std::string::size_type pos;
       while ( (pos = line.find(wordToBeDeleted)) != std::string::npos )
       {
          line.replace(pos, wordToBeDeleted.length(), wordToReplaceWith);
       }
       newFile << line << '\n';
    }
    
    while(getline(openningTheFile,line))
    { 
    std::string::size\u type pos;
    while((pos=line.find(wordToBeDeleted))!=std::string::npos)
    {
    line.replace(pos,wordToBeDeleted.length(),wordtoreplacetwith);
    }
    
    newFile这里有一个解决方案,它使用的强大功能以高级别但仍然非常灵活的方式解决任务

    对于OP来说,这很可能就像用大锤敲开螺母一样,但如果需要灵活性或必须处理更复杂的情况,它可能是正确的工具

    我使用的是a与a的组合。这允许我们动态地用替换字符串替换搜索模式,而无需创建任何中间字符串

    #include <iostream>
    #include <string>
    #include <sstream>
    #include <boost/iostreams/filtering_stream.hpp>
    #include <boost/iostreams/filter/regex.hpp>
    #include <boost/iostreams/copy.hpp>
    #include <boost/regex.hpp>
    namespace io = boost::iostreams;
    using namespace std;
    
    int main()
    { 
        // Your input "file" - you may replace it by an std::ifstream object.
        istringstream in( "why waste time learning learninglearning, when ignorance is instantaneous?" );
    
        // Search pattern and substitution string.        
        string findRegEx( R"(\blearning\b)" );
        string replaceStr( "sleeping" );
    
        // Build a regular expression filter and attach it to the input stream.
        io::filtering_istream inFiltered;
        inFiltered.push( io::regex_filter( boost::regex( findRegEx ), replaceStr ) );
        inFiltered.push( in );
    
        // Copy the filtered input to the output, replacing the search word on-the-fly.
        // Replace "cout" by your output file, e. g. an std::ofstream object.
        io::copy( inFiltered, cout );
    
        cout << endl;
    }
    
    注意事项:

    why waste time sleeping learninglearning, when ignorance is instantaneous?
    
    • 实际的正则表达式是
      \blearning\b
    • 我们不需要避开反斜杠,因为我们使用的是一个。非常整洁的
    • 正则表达式搜索整个单词“learning”(
      \b
      表示单词边界)。这就是为什么它只替换第一次出现的“learning”而不是“learninglearning”

    这不会删除输出中单词之间的所有空白吗?@zett42哦,是的。现在您正在将输入中的相邻ws折叠为输出中的单个ws。仍然具有相当大的破坏性;-)@zett42已修复。但不会保留任何额外的空白。您是否可以详细介绍如何使用stringstream从字符串中提取单词替换掉了吗?@mohsen,这在我的帖子中得到了回答。你还需要保留空白吗?@πάνταῥεῖ 我需要创建的新文件正好是迄今为止更改的最佳答案,因为该方法保留了所有空白。您不能将声明放入表达式中。但它不处理复合词。例如,如果用户希望替换单词“fire”,您的算法也会替换单词“firefly”的一部分@zett42,没错。希望OP能够修改他们的代码来处理它。