reread line using fstream getline

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

reread line using fstream getline

 
0
  #1
Jul 8th, 2009
Hi I got a code that in pseudo dos something like

  1. ifstream os;
  2. while(os.oef()){
  3. os.getline(buffer,length);
  4. }

If some condition is met,
I'd like to be able to jump back to the previous line,
such that the next getline will give the line just-read in, at a later time in my execution timeline.

Is this possible?

thanks
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 464
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: reread line using fstream getline

 
0
  #2
Jul 8th, 2009
Read
tellg and seekg methods.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: reread line using fstream getline

 
1
  #3
Jul 8th, 2009
Use two buffers or even better two std::strings:

  1. ifstream os; // variable name misleading!
  2. string prev, curr;
  3. while(getline(os, curr))
  4. {
  5. ...
  6. prev = curr;
  7. }
Last edited by jencas; Jul 8th, 2009 at 8:04 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: reread line using fstream getline

 
0
  #4
Jul 8th, 2009
To the OP:
Don't write your code like this: while(os.eof()) , instead write it like jencas did.
Want to know why? Do a forum search on: feof or eof

Edit:: I thought it was safely to assume that the 'oef' in 'os.oef()' is a typo he made.
(Because AFAIK there is no such data member in the iostream class library)
Last edited by tux4life; Jul 8th, 2009 at 11:35 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC