Start fgets at nth line?

This is a discussion on Start fgets at nth line? within the C Programming forums, part of the General Programming Boards category; Hi, when using fgets it reads the first line of the file, but say the first 20 lines of the ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    Start fgets at nth line?

    Hi, when using fgets it reads the first line of the file, but say the first 20 lines of the file are not of interest to a particular part of the program, then how can we read the 21st, or any other specified line? fgets doesn't seem to provide an option for this, so can it only be implemented using loops/counters?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no standard way to do that - if you know the size of your lines, it is POSSIBLE that you could use fseek() to get to the nth line. But the common way to get to the nth line is to simply read n lines using (for example) fgets.

    Edit: And before someone else points it out, yes, fseek() on text-files is undefined for all but either: values got from ftell() or 0, and only SEEK_SET is defined under these circumstances. Using any other variation on fseek() is undefined, and may work, or may crash the system, or do something else [most likely some arbitrarily "wrong" thing, or nothing]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Thanks, I'll have a look at fseek, though the size of the lines I want to miss can vary from file to file. I guess if reading those lines with fgets works, then I'll just do that.
    PHI is one 'H' of alot more interesting than PI!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by bertazoid View Post
    Thanks, I'll have a look at fseek, though the size of the lines I want to miss can vary from file to file. I guess if reading those lines with fgets works, then I'll just do that.
    fgets n times with a long enough buffer should be the right thing to do in this case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 02:30 PM
  3. help with fgets and assigning line to variable
    By pabl_o in forum C Programming
    Replies: 5
    Last Post: 04-11-2005, 10:20 PM
  4. Next Question...
    By Azmeos in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2003, 03:40 PM
  5. Line pointing at mouse (just to start)
    By IwillRegisterSoon in forum Game Programming
    Replies: 8
    Last Post: 09-01-2001, 11:10 AM

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