Position FILE pointer to a specified line of Text file

This is a discussion on Position FILE pointer to a specified line of Text file within the C Programming forums, part of the General Programming Boards category; Hi there, I am trying to read certain lines from a hugh text file. Each line has an line index ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    Position FILE pointer to a specified line of Text file

    Hi there,

    I am trying to read certain lines from a hugh text file. Each line has an line index number and a second number which is what I want to extract.

    I know which lines I want, is there a way to locate the FILE pointer directly to the lines? It takes way too long if I read in each line from the beginning and try to find the one I need because the text file is huge.

    Thanks a lot!

    JX

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't. fseek doesn't work with text files correctly. Loop through readind characters or lines at a time until you end up where it is you want to be.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Well if you read the file once (using fgets to read a line), and call ftell() before you call fgets(), then you can create an array of file positions corresponding to the start of each line.

    You can then use this record of stored ftell() positions in fseek() calls.
    Like so
    Writing to a text file
    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. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  2. Reading random line from a text file
    By helloamuro in forum C Programming
    Replies: 24
    Last Post: 05-03-2008, 10:57 PM
  3. reading from text file
    By jamez in forum C Programming
    Replies: 3
    Last Post: 11-30-2005, 06:13 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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