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

前端 未结 5 1115
广开言路
广开言路 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:02

    You seem to speak of elegance as a definite property of "little code". This is ofcourse subjective in some extent. Some would say that omitting all error handling isn't very elegant. Some would say that clear and compact code you understand right away is elegant.

    Write your own one-liner function/method which reads the file contents, but make it rigorous and safe underneath the surface and you will have covered both aspects of elegance.

    All the best

    /Robert

提交回复
热议问题