Copy data from fstream to stringstream with no buffer?

后端 未结 5 1360
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 19:02

Is there anyway I can transfer data from an fstream (a file) to a stringstream (a stream in the memory)?

Currently, I\'m using a buffer, but th

5条回答
  •  旧巷少年郎
    2021-02-01 19:23

    The only way using the C++ standard library is to use a ostrstream instead of stringstream.

    You can construct a ostrstream object with your own char buffer, and it will take ownership of the buffer then (so no more copying is needed).

    Note however, that the strstream header is deprecated (though its still part of C++03, and most likely, it will always be available on most standard library implementations), and you will get into big troubles if you forget to null-terminate the data supplied to the ostrstream.This also applies to the stream operators, e.g: ostrstreamobject << some_data << std::ends; (std::ends nullterminates the data).

提交回复
热议问题