Why isnt the >> working properly C++? -
std::wifstream thefilehandle; std::wstring thedata; thefilehandle.open( thefile.name() ); thefilehandle >> thedata; thefilehandle.close(); could tell me why string (thedata) getting first word file (thefile) ??? string contain of text file including white spaces , new lines, have suggestion this? thank you.
ps. need data preserved perfectly. thanks.
the reason you're getting first word precisely how >> operator works when applied strings - gets first whitespace-delimited token whatever stream you're reading from, after skipping leading whitespace.
if want read entire contents of file, can use getline function this:
std::wifstream thefilehandle; thefilehandle.open( thefile.name() );  std::wstringstream data; (std::wstring line; getline(thefilehandle, line); )     data << line << l"\n";  std::wstring thedata = data.str(); this loops while more data can read via getline , pulls data file.  since getline skips on newlines, approach adds newlines in.
edit: pointed out @pigben, there cleaner way using rdbuf():
std::wifstream thefilehandle; thefilehandle.open( thefile.name() );  std::wstringstream data; data << thefilehandle.rdbuf();  std::wstring thedata = data.str(); this uses fact stream insertion operator overloaded take in stream buffer. behavior in case read entire contents of stream buffer until of data has been exhausted, behavior want.
Comments
Post a Comment