C++ 如何一次逐行读取或读取整个文本文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13035674/
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 16:54:05  来源:igfitidea点击:

How to read line by line or a whole text file at once?

c++iostreamfstreamfile-handling

提问by Mody

I'm in a tutorial which introduces files (how to read and write from\to file)

我在一个介绍文件的教程中(如何从\到文件读取和写入)

First of all, this is not a homework, this is just general help I'm seeking.

首先,这不是作业,这只是我正在寻求的一般帮助。

I know how to read one word at a time, but I don't know how to read one line at a time or how to read the whole text file.

我知道如何一次阅读一个单词,但我不知道如何一次阅读一行或如何阅读整个文本文件。

What if my file contains 1000 words? It is not practical to read each word.

如果我的文件包含 1000 个单词怎么办?逐字逐句阅读是不切实际的。

My text file named (Read) contains the following:

我的名为 (Read) 的文本文件包含以下内容:

I love to play games I love reading I have 2 books

我喜欢玩游戏 我喜欢阅读 我有两本书

This is what I have accomplished so far:

这是我迄今为止所完成的:

#include <iostream>
#include <fstream>

using namespace std;
int main (){

  ifstream inFile;
  inFile.open("Read.txt");

  inFile >>

Is there any possible way to read the whole file at once, instead of reading each line or each word separate?

有没有可能一次读取整个文件,而不是单独读取每一行或每个单词?

回答by 111111

You can use std::getline:

您可以使用std::getline

#include <fstream>
#include <string>

int main() 
{ 
    std::ifstream file("Read.txt");
    std::string str; 
    while (std::getline(file, str))
    {
        // Process str
    }
}

Also note that it's better you just construct the file stream with the file names in it's constructor rather than explicitly opening (same goes for closing, just let the destructor do the work).

另请注意,最好在其构造函数中使用文件名构造文件流,而不是显式打开(关闭也是如此,只需让析构函数完成工作)。

Further documentation about std::string::getline()can be read at CPP Reference.

有关更多文档,请std::string::getline()参阅CPP 参考

Probably the easiest way to read a whole text file is just to concatenate those retrieved lines.

读取整个文本文件的最简单方法可能只是连接那些检索到的行。

std::ifstream file("Read.txt");
std::string str;
std::string file_contents;
while (std::getline(file, str))
{
  file_contents += str;
  file_contents.push_back('\n');
}  

回答by user2673553

I know this is a really really old thread but I'd like to also point out another way which is actually really simple... This is some sample code:

我知道这是一个非常古老的线程,但我还想指出另一种实际上非常简单的方法......这是一些示例代码:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main() {

    ifstream file("filename.txt");
    string content;

    while(file >> content) {
        cout << content << ' ';
    }
    return 0;
}

回答by AnkitSablok

Well, to do this one can also use the freopen function provided in C++ - http://www.cplusplus.com/reference/cstdio/freopen/and read the file line by line as follows -:

好吧,要做到这一点,也可以使用 C++ 中提供的 freopen 函数 - http://www.cplusplus.com/reference/cstdio/freopen/并按如下方式逐行读取文件 -:

#include<cstdio>
#include<iostream>

using namespace std;

int main(){
   freopen("path to file", "rb", stdin);
   string line;
   while(getline(cin, line))
       cout << line << endl;
   return 0;
}

回答by Bartek Banachewicz

I think you could use istream .read() function. You can just loop with reasonable chunk size and read directly to memory buffer, then append it to some sort of arbitrary memory container (such as std::vector). I could write an example, but I doubt you want a complete solution; please let me know if you shall need any additional information.

我认为你可以使用 istream .read() 函数。您可以使用合理的块大小循环并直接读取到内存缓冲区,然后将其附加到某种任意内存容器(例如 std::vector)。我可以写一个例子,但我怀疑你想要一个完整的解决方案;如果您需要任何其他信息,请告诉我。

回答by Bugster

Another method that has not been mentioned yet is std::vector.

另一种尚未提及的方法是std::vector.

std::vector<std::string> line;

while(file >> mystr)
{
   line.push_back(mystr);
}

Then you can simply iterate over the vector and modify/extract what you need/

然后您可以简单地遍历向量并修改/提取您需要的内容/