Thread: Position FILE pointer to a specified line of Text file

  1. #1
    Registered User
    Join Date
    Feb 2005

    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!


  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    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.

    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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
    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.

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, 07:13 PM
  4. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM