How do I back up a line with ifstream?

This is a discussion on How do I back up a line with ifstream? within the C++ Programming forums, part of the General Programming Boards category; Hello. I want to read the previous line with ifstream, while somewhere in the middle of the file. How do ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    How do I back up a line with ifstream?

    Hello.
    I want to read the previous line with ifstream, while somewhere in the middle of the file.

    How do I do this?

    Thanks in advance.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    tellg Get position of the get pointer. (public member function)
    seekg Set position of the get pointer (public member function )
    fstream - C++ Reference

    Use tellg() before you read a line, and store the answer somewhere.
    Passing that value to seekg() will allow you to re-read the same line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Salem View Post
    tellg Get position of the get pointer. (public member function)
    seekg Set position of the get pointer (public member function )
    fstream - C++ Reference

    Use tellg() before you read a line, and store the answer somewhere.
    Passing that value to seekg() will allow you to re-read the same line.
    Thanks.
    That worked.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read a line and go back to start
    By rocketman03 in forum C++ Programming
    Replies: 5
    Last Post: 02-11-2010, 02:24 AM
  2. How to set win32 console back to the first line?
    By dxfoo in forum Windows Programming
    Replies: 6
    Last Post: 03-08-2008, 11:47 AM
  3. going back to previous line
    By sankul in forum C Programming
    Replies: 5
    Last Post: 08-28-2007, 11:55 PM
  4. deleting ifstream line
    By jed in forum C++ Programming
    Replies: 8
    Last Post: 10-26-2006, 02:55 PM
  5. back a line
    By madmardigan53 in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-04-2003, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21