您的位置:首页 > 其它

Get an istream from a char*

2015-12-07 17:05 393 查看
#include <iostream>
#include <istream>
#include <streambuf>
#include <string>

struct membuf : std::streambuf
{
membuf(char* begin, char* end) {
this->setg(begin, begin, end);
}
};

int main()
{
char buffer[] = "I'm a buffer with embedded nulls\0and line\n feeds";

membuf sbuf(buffer, buffer + sizeof(buffer));
std::istream in(&sbuf);
std::string line;
while (std::getline(in, line)) {
std::cout << "line: " << line << "\n";
}
return 0;
}


Which outputs:
line: I'm a buffer with embedded nullsand line
line:  feeds
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: