The reason you're only getting the first word is that this is precisely how the >>
operator works when applied to strings - it just gets the first whitespace-delimited token from whatever stream you're reading from, after skipping any leading whitespace.
If you want to read the entire contents of the file, you can use the getline
function like this:
std::wifstream theFileHandle;
theFileHandle.open( theFile.Name() );
std::wstringstream data;
for (std::wstring line; getline(theFileHandle, line); )
data << line << L"\n";
std::wstring theData = data.str();
This loops while more data can be read via getline
and thus pulls all the data from the file. Since getline
skips over newlines, this approach also adds the newlines back in.
EDIT: As pointed out by @PigBen, there is a much cleaner way to do this using rdbuf()
:
std::wifstream theFileHandle;
theFileHandle.open( theFile.Name() );
std::wstringstream data;
data << theFileHandle.rdbuf();
std::wstring theData = data.str();
This uses the fact that the stream insertion operator is overloaded to take in a stream buffer. The behavior in this case is to read the entire contents of the stream buffer until all of the data has been exhausted, which is exactly the behavior you want.