getline failing to read from data file?

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: getline failing to read from data file?

 
0
  #11
Apr 14th, 2008
n8makar - congratulations on finding your own solution. I'm surprised the others who responded didn't spot that.

Mixing input with the extraction operator ( >> ) and getlline is a frequent source of problems for beginning students, that left over newline causes no end of frustration.

Now consider what happens if data is like:
  1. 12345 This is the name of the thing
  2. 8765 Some other thing
Your use of fin.ignore( ) will throw away the data we actually want.
An alternative means of removing the whitespace (blanks, tabs, newlines) is the ws manipulator. You might use it like:
  1. fin >> num_var;
  2. fin >> ws; //eats up whitspace till displayable char
  3. getline( fin, string_var );
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 2456 | Replies: 10
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC