how to put back into fstream the last character read

I have been struggling with this for a while. I would like to put back the last character read by file.get() ( an fstream object ) because the next lines in the file are not organized by ('s nor are they integers...

Is there a simple way of doing this? file.put() and file.putback() both seem to be mucking the next several lines of the file input up...

Any help would be greatly appreciated here ( the rest of the function which is getting the integers from the file and discarding the ('s is working fine).

The reason I want to put it back is that the next function is already working perfectly uses file.getline() to process each line as a character string ( which the transTable machine processes ).
Last edited on
Hello again,

Are you allowed to use the STL - I think I asked last time?

Then you can use getline and parse the string from there, (with the STL string member functions and or algorithms), not to worry about consuming or putting back chars.

Regards
Thank again - going to look into STL soon.

Ended up fixing it using file.peek()...
Last edited on
Ok.

Can you show the code with the putback function call? Just trying to understand how that messes up the other input.

How did you get on with the debugger in this scenario?

Just to reiterate: the sooner you are using the STL, the easier your life will be. One can get away form the "C" approach of reading each char.
Non-modifying putback of the last character that was extracted is supported on input file streams.
http://en.cppreference.com/w/cpp/io/basic_istream/putback
http://en.cppreference.com/w/cpp/io/basic_filebuf/pbackfail
Using peek allowed me to see the character without getting it.

I took your suggestion and learned the netbeans debugger which ended up not taking more than a half an hour or so ( well at least the functionality I use, i.e. viewing variable changes and setting break points, etc. ).
Last edited on
> Using peek allowed me to see the character without getting it.

Yes, peek() is an alternative.

get() and then an unget() http://en.cppreference.com/w/cpp/io/basic_istream/unget can also be used.


> netbeans debugger which ended up not taking more than a half an hour or so ...

Java pigs need to be killed and restarted periodically.
I was using eclipse with same c++ plug in and when my program crashed it hung the entire Windows8 operating system.. At least netBeans lets me kill the process and continue...
Topic archived. No new replies allowed.