【问题标题】:Detect new line c++ fstream检测新行 c++ fstream
【发布时间】:2013-08-19 22:32:53
【问题描述】:

如何通过使用 fstream 将内容复制到另一个 .txt 来读取 .txt 到类似内容。 问题是,当文件中有新行时。使用 ifstream 时如何检测?

用户输入“苹果”

例如: note.txt => 我昨天买了一个苹果。 苹果味道鲜美。

note_new.txt => 我昨天买了一个。 味道鲜美。

结果注释假设在上面,但是: note_new.txt => 我昨天买了一个。味道鲜美。

如何检查源文件中是否有新行,它也会在新文件中创建新行。

这是我当前的代码:

#include <iostream>
#include <fstream>
#include <string> 

using namespace std;

int main() {
    ifstream inFile ("note.txt");
    string word;
    ofstream outFile("note_new.txt");

    while(inFile >> word) {
        outfile << word << " ";
    }
}

你们都可以帮助我吗?实际上,我还会检查检索到的单词何时与用户指定的单词相同,然后我不会在新文件中写入该单词。所以一般来说,它会删除与用户指定的单词相同的单词。

【问题讨论】:

  • 提示:std::getline()
  • 您逐字解析文件是否有原因?获取相同文件的最简单方法是根本不解析,只读取/写入二进制数据或使用任何平台特定的 API 来复制文件。
  • @RetiredNinja 实际上我还会检查检索到的单词何时与用户指定的相同,然后我不会在新文件中写入该单词。所以一般来说,它会删除与用户指定的单词相同的单词。
  • 您应该在问题中指定它,因为它不适合“完全相同的内容”部分。

标签: c++ fstream file-io


【解决方案1】:

逐行法

如果你还想逐行做,可以使用std::getline()

#include <iostream>
#include <fstream>
#include <string> 

using namespace std;

int main() {
    ifstream inFile ("note.txt");
    string line;
    //     ^^^^
    ofstream outFile("note_new.txt");

    while( getline(inFile, line) ) {
    //     ^^^^^^^^^^^^^^^^^^^^^
        outfile << line << endl;
    }
}

它从流中获取一行,你只需在任何你想要的地方重写它。


更简单的方法

如果您只想在另一个文件中重写一个文件,请使用rdbuf

#include <fstream>

using namespace std;

int main() {
    ifstream inFile ("note.txt");
    ofstream outFile("note_new.txt");

    outFile << inFile.rdbuf();
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^
}

编辑:它将允许删除您不想在新文件中出现的单词:

我们使用std::stringstream

#include <iostream>
#include <fstream>
#include <stringstream>
#include <string> 

using namespace std;

int main() {
    ifstream inFile ("note.txt");
    string line;
    string wordEntered("apple"); // Get it from the command line
    ofstream outFile("note_new.txt");

    while( getline(inFile, line) ) {

        stringstream ls( line );
        string word;

        while(ls >> word)
        {
            if (word != wordEntered)
            {
                 outFile << word;
            }
        }
        outFile << endl;
    }
}

【讨论】:

    【解决方案2】:

    有一种更简单的方法来完成这项工作:

    #include <fstream>
    
    int main() {
        std::ifstream inFile ("note.txt");
        std::ofstream outFile("note_new.txt");
    
        outFile << inFile.rdbuf();
    }
    

    【讨论】:

      【解决方案3】:

      如果您想从输入文件中删除文本(正如您的描述所暗示但未说明的那样)。

      然后你需要逐行阅读。但是接下来需要逐字解析每一行,以确保您可以删除您正在寻找的工作apple

      #include <fstream>
      #include <string> 
      
      using namespace std;
      // Don't do this. 
      
      int main(int argc, char* argv[])
      {
          if (argv == 1) { std::cerr << "Usage: Need a word to remove\n";exit(1);}
          std::string userWord = argv[1];  // Get user input (from command line)
      
          std::ifstream inFile("note.txt");
          std::ofstream outFile("note_new.txt");
          std::string   line;
      
          while(std::getline(inFile, line))
          {
               // Got a line
               std::stringstream linestream(line);
               std::string  word;
      
               while(linestream >> word)
               {
                      // Got a word from the line.
                      if (word != userWord)
                      {
                           outFile << word;
                      }
               }
               // After you have processed each line.
               // Add a new line to the output.
               outFile << "\n";
          }
      }
      

      【讨论】:

      • 您可能希望在单词之间添加空格,很可能是在每行的第一个标记之后写入每个标记之前,以防止在行尾出现尾随空格。即使那样,如果这很重要,它也不会在文件中保留任何额外的空格。
      • @RetiredNinja:确实。还需要补偿标点符号。但我不能什么都做。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-27
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多