Get an istream from a char*

来源:互联网 发布:达内python视频下载 编辑:程序博客网 时间:2024/05/24 01:01
#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 lineline:  feeds
1 0
原创粉丝点击