C++ 如何将整个流读入 std::string?

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

How to read entire stream into a std::string?

c++stringstream

提问by Roddy

I'm trying to read an entire stream (multiple lines) into a string.

我正在尝试将整个流(多行)读入一个字符串。

I'm using this code, and it works, but it's offending my sense of style... Surely there's an easier way? Maybe using stringstreams?

我正在使用这段代码,它可以工作,但它冒犯了我的风格……当然有更简单的方法吗?也许使用字符串流?

void Obj::loadFromStream(std::istream & stream)
{ 
  std::string s;

  std::streampos p = stream.tellg();  // remember where we are

  stream.seekg(0, std::ios_base::end); // go to the end
  std::streamoff sz = stream.tellg() - p;  // work out the size
  stream.seekg(p);        // restore the position

  s.resize(sz);          // resize the string
  stream.read(&s[0], sz);  // and finally, read in the data.



实际上,constconst对字符串的引用也可以,这可能会使事情变得更容易......

const std::string &s(... a miracle occurs here...)

回答by Cubbi

How about

怎么样

std::istreambuf_iterator<char> eos;
std::string s(std::istreambuf_iterator<char>(stream), eos);

(could be a one-liner if not for MVP)

(如果不是MVP,可能是单线)

post-2011 edit, this approach is now spelled

2011 年之后的编辑,这种方法现在拼写

std::string s(std::istreambuf_iterator<char>(stream), {});

回答by Joey Adams

I'm late to the party, but here is a fairly efficient solution:

我迟到了,但这是一个相当有效的解决方案:

std::string gulp(std::istream &in)
{
    std::string ret;
    char buffer[4096];
    while (in.read(buffer, sizeof(buffer)))
        ret.append(buffer, sizeof(buffer));
    ret.append(buffer, in.gcount());
    return ret;
}

I did some benchmarking, and it turns out that the std::istreambuf_iteratortechnique (used by the accepted answer) is actually much slower. On gcc 4.4.5 with -O3, it's about a 4.5x difference on my machine, and the gap becomes wider with lower optimization settings.

我做了一些基准测试,结果发现该std::istreambuf_iterator技术(被接受的答案使用)实际上要慢得多。在带有 的 gcc 4.4.5 上-O3,在我的机器上大约有 4.5 倍的差异,并且随着优化设置的降低,差距变得更大。

回答by Matteo Italia

You could do

你可以做

std::string s;
std::ostringstream os;
os<<stream.rdbuf();
s=os.str();

but I don't know if it's more efficient.

但我不知道它是否更有效率。

Alternative version:

替代版本:

std::string s;
std::ostringstream os;
stream>>os.rdbuf();
s=os.str();

回答by wheaties

You can try using something from algorithms. I have to get ready for work but here's a very quick stab at things (there's got to be a better way):

您可以尝试使用算法中的某些内容。我必须为工作做好准备,但这里有一个非常快速的尝试(必须有更好的方法):

copy( istreambuf_iterator<char>(stream), istreambuf_iterator<char>(), back_inserter(s) );

回答by wdavilaneto

Well, if you are looking for a simple and 'readable' way to do it. I would recomend add/use some high level framework on your project. For that I's always use Poco and Boost on all my projects. In this case, with Poco:

好吧,如果您正在寻找一种简单且“可读”的方式来做到这一点。我建议在您的项目中添加/使用一些高级框架。为此,我总是在我所有的项目中使用 Poco 和 Boost。在这种情况下,使用 Poco:

    string text;
    FileStream fstream(TEXT_FILE_PATH);
    StreamCopier::copyToString(fstream, text);

回答by user7223715

Perhaps this 1 line C++11 solution:

也许这 1 行 C++11 解决方案:

std::vector<char> s{std::istreambuf_iterator<char>{in},{}};