What is the most elegant way to read a text file with c++?

前端 未结 5 1111
广开言路
广开言路 2020-11-28 00:49

I\'d like to read whole content of a text file to a std::string object with c++.

With Python, I can write:

text = open(\"text.txt\", \"         


        
5条回答
  •  爱一瞬间的悲伤
    2020-11-28 01:22

    There's another thread on this subject.

    My solutions from this thread (both one-liners):

    The nice (see Milan's second solution):

    string str((istreambuf_iterator(ifs)), istreambuf_iterator());
    

    and the fast:

    string str(static_cast(stringstream() << ifs.rdbuf()).str());
    

提交回复
热议问题