How to read previous line of file?

This is a discussion on How to read previous line of file? within the C++ Programming forums, part of the General Programming Boards category; Ok, so I was wondering how would I read a previous line of a file I already read with getline( ...

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

    Arrow How to read previous line of file?

    Ok, so I was wondering how would I read a previous line of a file I already read with getline(ifstream_object, buffer_str)?
    I figured seekg(-1, ios_base::cur) might work to step to the position of the previous line, but since I don't have much experience using seekg() and the reference that I read on it didn't mention how the first parameter "streampos off" is calculated (i.e. if its relative to the line read or if its relative to the character position), I figured I would ask here.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    999
    The easiest way would be to store the previous line as you step through, i.e. read in the current line, and at the end of your loop store curr -> last
    Last edited by Epy; 01-20-2012 at 09:37 PM.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,013
    Obviously it's relative to the current character position, not line.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,830
    Saving the line as Epy suggests is the simplest thing.

    Or you could do -> istream::tellg - C++ Reference
    Code:
    streampos pos = fs.tellg();
    // read a line
    ...
    fs.seekg(pos, ios::beg);
    // read a line again.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 08-07-2011, 10:55 PM
  2. Read file line by line and interpret them
    By sombrancelha in forum C Programming
    Replies: 8
    Last Post: 03-17-2011, 10:48 AM
  3. Read text file line by line and write lines to other files
    By magische_vogel in forum C Programming
    Replies: 10
    Last Post: 01-23-2011, 10:51 AM
  4. How to go to a previous line
    By Megamanenm in forum C++ Programming
    Replies: 1
    Last Post: 04-14-2009, 07:49 PM
  5. Returning to previous line
    By Garland in forum C++ Programming
    Replies: 7
    Last Post: 01-12-2008, 06:15 AM

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