943,649 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1924
  • C++ RSS
Jul 8th, 2009
0

reread line using fstream getline

Expand Post »
Hi I got a code that in pseudo dos something like

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 70
Solved Threads: 9
Junior Poster
monkey_king is offline Offline
160 posts
since Aug 2008
Jul 8th, 2009
0

Re: reread line using fstream getline

Read
tellg and seekg methods.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Jul 8th, 2009
1

Re: reread line using fstream getline

Use two buffers or even better two std::strings:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Jul 8th, 2009
0

Re: reread line using fstream getline

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.
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Questions on vector
Next Thread in C++ Forum Timeline: vs2008 fstream open() fails





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC