C++ stl stringstream direct buffer access

后端 未结 4 1515
囚心锁ツ
囚心锁ツ 2020-12-29 20:31

this should be pretty common yet I find it fascinating that I couldn\'t find any straight forward solution.

Basically I read in a file over the network into a string

4条回答
  •  渐次进展
    2020-12-29 20:36

    You can call str() to get back a std::string. From there you can call c_str() on the std::string to get a char*. Note that c_str() isn't officially supported for this use, but everyone uses it this way :)

    Edit

    This is probably a better solution: std::istream::read. From the example on that page:

      buffer = new char [length];
    
      // read data as a block:
      is.read (buffer,length);
    

提交回复
热议问题