I would indent your code, so it's a little easier to read. I generally always put my loops within braces, but for one-liners you don't need that.
1 2 3 4 5 6 7
|
Text(string filename)
{
string line;
ifstream f(filename.c_str());
while (getline(f, line))
text += line;
}
|
Do you want to read in the file
exactly as it appears? If so, you should do what JLBorges showed above and read the file character-by-character so you pick up every whitespace character (including newline characters).
Also like JLBorges showed above, you should use
default
if you wish to have the default constructor made for you:
It isn't explicitly necessary, but it is in line with the new standard as of C++11.
http://en.cppreference.com/w/cpp/keyword/default
Also, close the file stream when you're finished with it
f.close()
.
JLBorges's example is a very good one to follow. The only line you might have trouble with is this one
txt = { std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>() ; }
, to which he presented an alternative.