2

Possible Duplicate:
Read whole ASCII file into C++ std::string

C#

string contents = File.ReadAllText(filename);

Python

contents = open(filename).read()

Do we really have nothing so simple and concise like that in C++? The best I've seen is:

string slurp(const std::string& filename) {
    ifstream ifs("test.txt", ifstream::in);
    stringstream sstr;
    sstr << ifs.rdbuf();
    return sstr.str();
}

Which is shameful in a language that's been around so long, with both boost and the stdlib at one's disposal. Easy things should be easy...

Community
  • 1
  • 1
Eloff
  • 18,456
  • 14
  • 72
  • 96

0 Answers0