Thread: How do I back up a line with ifstream?

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.

  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, 03: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, 12:47 PM
  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