Finding a function to return the amount of lines in an input file

This is a discussion on Finding a function to return the amount of lines in an input file within the C Programming forums, part of the General Programming Boards category; I am trying to make some code that was handed to me more user friendly. I want to find a ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    14

    Finding a function to return the amount of lines in an input file

    I am trying to make some code that was handed to me more user friendly. I want to find a function that will scan the input file for the number of lines. Currently the first line of the input file contains the file length in lines. The rest of the code uses that number to iterate

    Code:
    	int inum_rows;		
    	fpin0 = fopen("table_eng_la1.dat","r");
    	fscanf(fpin0,"%d",&inum_rows);
    	for(i = 0; i < inum_rows; i++)
    	{
    		fscanf(fpin0,"%lf %lf %lf %lf",&table_eng0[i], &table_eng1[i],
    			&table_eng2[i], &table_eng3[i]);
    	}	
    	fclose(fpin0);
    	
    	for(i = 0; i < inum_rows; i++)
                    /*Do Something */
    Please make your recommendations simple....... I am not a programmer......... Thanx!!!!!!!!!

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    Originally posted by Salem
    So you want to count the number of lines in the file (and assign it to inum_rows), rather than reading inum_rows from the first line in the file?

    Exactly!!!

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I really shouldn't do your homework for you, but I can't resist:
    Code:
    #include <stdio.h>
    int main( void )
    {
        int c,count=0;
        /* open file 'fp' for reading */
        while((c=fgetc(fp))!=EOF)
            if(c=='\n')
                count++;
        /* close file, do something with count */
        return 0;
    }
    Enjoy.

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

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    14
    I wish it were homework.......then I would have an instructor to ask these questions. Actually, I have been out of college for many years (the reason I can't remember how to program in "C"). This is for a real world application......... thanks a ton for the info! I am sure I will be posting more in the days to come.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM

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