【发布时间】:2020-11-24 14:29:02
【问题描述】:
我需要用 istream 的前 N 个字节创建一个 std::string...我该怎么做?
std::istream istm;
std::string str;
istm >> str; //will read tons of stuff until finds whitespace
std::string str(N, ' ');
istm.read(str.data(), N); //can't write into buffer inside string, cause data() returns const char*
std::unique_ptr<char[N+1]> buf;
istm.read(buf.get(), N);
std::string str(buf.get()); //should work, but why extra buffer?
那么...我该怎么做才好呢?
【问题讨论】:
-
为什么不一次只读取一个字节,也许使用
for循环?