Reading directly from an std::istream into an std::string

后端 未结 6 1054
误落风尘
误落风尘 2020-11-30 08:00

Is there anyway to read a known number of bytes, directly into an std::string, without creating a temporary buffer to do so?

eg currently I can do it by



        
6条回答
  •  天命终不由人
    2020-11-30 08:22

    An easy way would be:

    std::istream& data
    const size_t dataSize(static_cast(data.rdbuf()->in_avail()));
    std::string content;
    content.reserve( dataSize);
    data.read(&content[0], dataSize);
    

提交回复
热议问题