Thread: Reg. fseek

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    Question Reg. fseek

    Hi Folks,

    I have a file with some records in it. As I process the records one by one I need to move the pointer to the beginning of the next record (next line). How do I implement this??? I used fseek but I couldn't implement it..

    The records in the file are not of constant length. Please help me out. thanks in advance.

    Pokks.

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    If the size of the records is not known in advance, the only way to do it is by starting at the beginning and reading one line at a time until you reach the line you're interested in. If you use newline termination on each record and your records are text, not binary, fgets() should work nicely for this.

    If this is something you do repeatedly, you can use ftell() before reading each line and build an index containing the line identifier (line #, key value, and/or some other such data) and the offset (from ftell()), and use the offset in calls to fseek() to position the file before a call to fgets() to read the record. (This a way to implement a simple ISAM text database, BTW.)

    If you're updating these records, keep in mind that you should not change the length of any record during the in-place update.
    Insert obnoxious but pithy remark here

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. fseek question
    By blondie.365 in forum C Programming
    Replies: 19
    Last Post: 10-20-2008, 04:30 PM
  3. fseek() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 02:30 PM
  4. Help With fseek();
    By Explicit in forum C Programming
    Replies: 3
    Last Post: 05-26-2004, 08:40 PM
  5. popen and fseek
    By mach5 in forum C Programming
    Replies: 4
    Last Post: 11-29-2003, 02:03 AM