How to read entire stream into a std::string?

后端 未结 7 990
灰色年华
灰色年华 2020-11-29 19:15

I\'m trying to read an entire stream (multiple lines) into a string.

I\'m using this code, and it works, but it\'s offending my sense of style... Surely there\'s an

7条回答
  •  独厮守ぢ
    2020-11-29 19:27

    You can try using something from algorithms. I have to get ready for work but here's a very quick stab at things (there's got to be a better way):

    copy( istreambuf_iterator(stream), istreambuf_iterator(), back_inserter(s) );
    

提交回复
热议问题