将整个ASCII文件读入C ++ std :: string [英] Read whole ASCII file into C++ std::string

查看:138
本文介绍了将整个ASCII文件读入C ++ std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将整个文件读入内存,并将其放在C ++ std :: string 中。

I need to read a whole file into memory and place it in a C++ std::string.

如果我把它读入一个 char [] ,答案会很简单:

If I were to read it into a char[], the answer would be very simple:

std::ifstream t;
int length;
t.open("file.txt");      // open input file
t.seekg(0, std::ios::end);    // go to the end
length = t.tellg();           // report location (this is the length)
t.seekg(0, std::ios::beg);    // go back to the beginning
buffer = new char[length];    // allocate memory for a buffer of appropriate dimension
t.read(buffer, length);       // read the whole file into the buffer
t.close();                    // close file handle

// ... Do stuff with buffer here ...

现在,我想做同样的事情,但使用 std :: string 而不是 char [] 。我想避免循环,即我不要要:

Now, I want to do the exact same thing, but using a std::string instead of a char[]. I want to avoid loops, i.e. I don't want to:

std::ifstream t;
t.open("file.txt");
std::string buffer;
std::string line;
while(t){
std::getline(t, line);
// ... Append line to buffer and go on
}
t.close()

任何想法?

推荐答案

更新:方法,同时遵循STL成语,实际上是令人惊讶的低效率!不要对大文件执行此操作。 (请参阅: http://insanecoding.blogspot.com/2011 /11/how-to-read-in-file-in-c.html

Update: Turns out that this method, while following STL idioms well, is actually surprisingly inefficient! Don't do this with large files. (See: http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html)

您可以从文件中创建一个streambuf迭代器并初始化具有它的字符串:

You can make a streambuf iterator out of the file and initialize the string with it:

#include <string>
#include <fstream>
#include <streambuf>

std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char>(t)),
                 std::istreambuf_iterator<char>());

不知道你在哪里得到 t.open txt,r)语法。据我所知,这不是一个 std :: ifstream 的方法。您似乎已将它与C的 fopen 混淆。

Not sure where you're getting the t.open("file.txt", "r") syntax from. As far as I know that's not a method that std::ifstream has. It looks like you've confused it with C's fopen.

编辑:请注意字符串构造函数的第一个参数周围的额外括号。 这些是必需的。它们可以防止称为最烦琐的解析的问题,在此case

Also note the extra parentheses around the first argument to the string constructor. These are essential. They prevent the problem known as the "most vexing parse", which in this case won't actually give you a compile error like it usually does, but will give you interesting (read: wrong) results.

按照KeithB的意见,这里的例子不会给你一个编译错误,因为它通常会,但会给你有趣的(读错了)结果。一种分配所有内存的方法(而不是依赖于字符串类的自动重新分配):

Following KeithB's point in the comments, here's a way to do it that allocates all the memory up front (rather than relying on the string class's automatic reallocation):

#include <string>
#include <fstream>
#include <streambuf>

std::ifstream t("file.txt");
std::string str;

t.seekg(0, std::ios::end);   
str.reserve(t.tellg());
t.seekg(0, std::ios::beg);

str.assign((std::istreambuf_iterator<char>(t)),
            std::istreambuf_iterator<char>());

这篇关于将整个ASCII文件读入C ++ std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆