Reading line by line in a large text file

Discussion in 'C++' started by xyz, Jun 9, 2008.

  1. xyz

    xyz Guest

    Hi,
    I have a text file around 7GB includes 100 million lines...
    I want to read the data line by line when I approach my module..

    ie., when i read for the first time , my program shuld read only one
    line
    and when i read for the next time , my program shuld read only second
    line discarding first line..

    i did it with ignore function but it is taking lot of time to reach
    end of file...

    please give me a simple approch to do this..

    thanks to all
     
    xyz, Jun 9, 2008
    #1
    1. Advertising

  2. xyz

    Lars Uffmann Guest

    xyz wrote:
    > I have a text file around 7GB includes 100 million lines...
    > I want to read the data line by line when I approach my module..
    >
    > ie., when i read for the first time , my program shuld read only one
    > line and when i read for the next time , my program shuld read only
    > second line discarding first line..


    I'm not sure what you mean by "first time" and "next time" - do you mean
    subsequent read calls with the open file, or do you mean reading a line,
    closing the file, then opening it again and now reading the 2nd line?

    In the first case, I don't see the problem, because you just read one
    line after another.

    In the second case, you'll need to store a filepointer somewhere
    (variable or config file) - to the position AFTER your last read (i.e.
    the first byte of the succeeding line). Then when you re-open the text
    file, just jump to that file pointer and continue reading.

    For info on ftell & fseek, check the c++ reference:
    http://www.cplusplus.com/reference/clibrary/cstdio/ftell.html


    Best Regards,

    Lars
     
    Lars Uffmann, Jun 9, 2008
    #2
    1. Advertising

  3. xyz

    xyz Guest

    On Jun 9, 2:09 pm, Lars Uffmann <> wrote:
    > xyz wrote:
    > > I have a text file around 7GB includes 100 million lines...
    > > I want to read the data line by line when I approach my module..

    >
    > > ie., when i read for the first time , my program shuld read only one
    > > line and when i read for the next time , my program shuld read only
    > > second line discarding first line..

    >
    > I'm not sure what you mean by "first time" and "next time" - do you mean
    > subsequent read calls with the open file, or do you mean reading a line,
    > closing the file, then opening it again and now reading the 2nd line?
    >
    > In the first case, I don't see the problem, because you just read one
    > line after another.
    >
    > In the second case, you'll need to store a filepointer somewhere
    > (variable or config file) - to the position AFTER your last read (i.e.
    > the first byte of the succeeding line). Then when you re-open the text
    > file, just jump to that file pointer and continue reading.
    >
    > For info on ftell & fseek, check the c++ reference:http://www.cplusplus.com/reference/clibrary/cstdio/ftell.html
    >
    > Best Regards,
    >
    >    Lars


    What i mean is....i want the second case...
    i tried storing the file positon after reading the line...
    but i couldnt succeed...
    could you please get some more details
     
    xyz, Jun 9, 2008
    #3
  4. xyz

    xyz Guest

    On Jun 9, 2:17 pm, xyz <> wrote:
    > On Jun 9, 2:09 pm, Lars Uffmann <> wrote:
    >
    >
    >
    > > xyz wrote:
    > > > I have a text file around 7GB includes 100 million lines...
    > > > I want to read the data line by line when I approach my module..

    >
    > > > ie., when i read for the first time , my program shuld read only one
    > > > line and when i read for the next time , my program shuld read only
    > > > second line discarding first line..

    >
    > > I'm not sure what you mean by "first time" and "next time" - do you mean
    > > subsequent read calls with the open file, or do you mean reading a line,
    > > closing the file, then opening it again and now reading the 2nd line?

    >
    > > In the first case, I don't see the problem, because you just read one
    > > line after another.

    >
    > > In the second case, you'll need to store a filepointer somewhere
    > > (variable or config file) - to the position AFTER your last read (i.e.
    > > the first byte of the succeeding line). Then when you re-open the text
    > > file, just jump to that file pointer and continue reading.

    >
    > > For info on ftell & fseek, check the c++ reference:http://www.cplusplus.com/reference/clibrary/cstdio/ftell.html

    >
    > > Best Regards,

    >
    > >    Lars

    >
    > What i mean is....i want the second case...
    > i tried storing the file positon after reading the line...
    > but i couldnt succeed...
    > could you please get some more details


    i got it...thanks
     
    xyz, Jun 9, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darrel
    Replies:
    3
    Views:
    701
    Kevin Spencer
    Nov 11, 2004
  2. kaushikshome
    Replies:
    4
    Views:
    824
    kaushikshome
    Sep 10, 2006
  3. alangs
    Replies:
    0
    Views:
    873
    alangs
    Jul 22, 2010
  4. Robin Wenger
    Replies:
    191
    Views:
    3,315
  5. curious
    Replies:
    1
    Views:
    194
    Patrick Spence
    Oct 25, 2006
Loading...

Share This Page