检测新行 c++ fstream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18324189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:54:22  来源:igfitidea点击:

Detect new line c++ fstream

c++fstreamfile-io

提问by muhihsan

How do I read a .txt copy the content to another .txt by using fstream to a similar content. The problem is, when in the file there is new line. How do I detect that while using ifstream?

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

user enter "apple"

用户输入“苹果”

Eg: note.txt => I bought an apple yesterday. The apple tastes delicious.

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

note_new.txt => I bought an yesterday. tastes delicious.

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

the resulting note suppose to be above, but instead: note_new.txt => I bought an yesterday. tastes delicious.

结果笔记应该在上面,而是:note_new.txt => 我昨天买了一个。味道鲜美。

How do I check if there is a new line in the source file, it also will create new line in new file.

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

Here is my current code:

这是我当前的代码:

#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 << " ";
    }
}

can you all help me? actually I also check when the word retrieved is the same as what user specified, then I won't write that word in the new file. So it general, it will delete words which are the same as the one specified by user.

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

回答by Pierre Fourgeaud

Line-by-line method

逐行方法

If you still want to do it line-by-line, you can use std::getline():

如果您仍然想逐行执行,则可以使用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;
    }
}

It gets a line from the stream and you just to rewrite it wherever you want.

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



Easier method

更简单的方法

If you just want to rewrite one file inside the other one, use rdbuf:

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

#include <fstream>

using namespace std;

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

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


EDIT :It will permit to remove the words you don't want to be in the new file :

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

We use std::stringstream:

我们使用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;
    }
}

回答by Jerry Coffin

There's a much easier way to do the job:

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

#include <fstream>

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

    outFile << inFile.rdbuf();
}

回答by Martin York

If you want to remove text from the input file (as your description suggests but does not state).

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

Then you need to read line by line. But then each line needs to be parsed word by word to make sure you can remove the work you are looking for apple.

然后你需要逐行阅读。但随后需要逐字解析每一行,以确保您可以删除您正在寻找的工作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";
    }
}