Thread: read multiple lines from a file

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    36

    read multiple lines from a file

    I'm trying to write a program that reads in multiple lines from a file...and I can't figure out how to set it to read in a specific number of lines...

    Let's say one of the parameters passed to my program says that it wants to read in the first 7 lines from the file. How do i do that?

    i know fgets reads in the first line...but where do i go after that?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a loop.
    Code:
    for( x = 0; x < numberoflinestoread; x++ )
    {
        ...read a line...
        ...do something with it...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    66
    you could also use a counter, counting the number of "line jumps", when this reaches the number you want (or if it reaches the EOF first) end your cycle.
    Code:
    x=0;
    while(x<number_of_lines && "name of the pointer to the file"!= EOF){
                
                 if("name of the pointer to the file"=EOF && x< number_of_lines-1)
                                 printf("COULDN'T READ THOSE MANY LINES");
                 else
                 fgets();
                 x++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Read lines from a file
    By steffi in forum C Programming
    Replies: 2
    Last Post: 11-13-2007, 06:05 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM