[英]How to read entire stream into a std::string?
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.
const
reference to a string would do as well, and that may make things easier...
const
引用也可以,这可能会使事情变得更容易......
const std::string &s(... a miracle occurs here...)
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), {});
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_iterator
technique ( used by the accepted answer ) is actually much slower.我做了一些基准测试,结果发现
std::istreambuf_iterator
技术( 被接受的答案使用)实际上要慢得多。 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.在带有
-O3
gcc 4.4.5 上,在我的机器上大约有 4.5 倍的差异,并且随着优化设置的降低,差距变得更大。
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();
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) );
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.
为此,我总是在我所有的项目中使用 Poco 和 Boost。 In this case, with Poco:
在这种情况下,使用 Poco:
string text;
FileStream fstream(TEXT_FILE_PATH);
StreamCopier::copyToString(fstream, text);
What about to use getline with delimiter?使用带有分隔符的 getline 怎么样? The next code helps me to read whole std::cin into string on ubuntu with g++-10.
下一个代码帮助我使用 g++-10 在 ubuntu 上将整个 std::cin 读入字符串。
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
getline(cin, s, {}); //the whole stream into variable s
return 0;
}
也许这 1 行 C++11 解决方案:
std::vector<char> s{std::istreambuf_iterator<char>{in},{}};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.