Reputation: 1027
std::wifstream theFileHandle;
std::wstring theData;
theFileHandle.open( theFile.Name() );
theFileHandle >> theData;
theFileHandle.close();
Could anyone tell me why my string (theData) is only getting the first word from the file (theFile) ??? I would like the string to contain all of the text from the file including white spaces and new lines, does anyone have a suggestion for this? Thank you.
PS. I need the data to be preserved perfectly. Thanks.
Upvotes: 1
Views: 316
Reputation: 490408
operator>>
for a string is defined to work pretty much the same as the %s
conversion for scanf
, which only reads until it encounters white space.
There are a number of ways of reading an entire file into a string. The simplest is probably:
std::wstringstream buffer;
buffer << theFileHandle.rdbuf();
theData = buffer.str();
If you're dealing with a big file, there's at least one way that's clearly faster. See Test Four in an answer I posted previously (but that code is Martin York's, not mine).
Upvotes: 3
Reputation: 373002
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.
Upvotes: 4
Reputation: 6834
See excellent previous answer for a concise way of reading in the whole file, and doing it efficiently.
Upvotes: 1
Reputation: 9172
That's how >>
is supposed to work. It extracts formatted data from the input stream. You want .read()
which extracts unformatted data.
see more here: http://www.cplusplus.com/reference/iostream/istream/read/
e.g.:
std::wifstream theFileHandle;
wchar_t theData[SOME_LARGE_VALUE];
theFileHandle.open( theFile.Name() );
theFileHandle.read(theData, sizeof(theData));
theFileHandle.close();
Upvotes: 1