Solved

returning number of lines in a file?

Posted on 2004-09-19
7
390 Views
Last Modified: 2010-04-15
The following statement reads from a file and outout the first 24 lines. However, i need to find the (%) read. So in order to find the % i need to get the total number of lines in the file . Is there any function which does that?
This is my statement.
Thanks in advance,

 while ( fgets( line, LINELEN, fp ) ){ /* more input */
              if ( num_of_lines == PAGELEN ) { /* full screen? */
                   reply = see_more(fp_tty); /* NEW: pass FILE * */
                   if ( reply == 0 ) /* n: done */
                           break;
                   num_of_lines -= reply; /* reset count */
              }
              if ( fputs( line, stdout ) == EOF ) /* show line*/
                   exit(1); /* or die */
              num_of_lines++; /* count it */
       }
0
Comment
Question by:Carl3003
  • 3
  • 3
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12098005
If your file has variable lenght lines, then there is not a function to give you number of lines. But you can give % read in this manner.
- Obtain total file length (T).
- Read every line and ACUMMULATE line lenghts in a variable (R)
- Your % read is R/T
0
 

Author Comment

by:Carl3003
ID: 12098018
Here comes my problem,
If i read the file once, I can not read it again.I do not know why. But here is an exammple.

while ( fgets( line, LINELEN, fp ) )
{

              total_lines++; /* count it */         
  }

then the next while loop would not work..
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 125 total points
ID: 12098061
call rewind(fp) after the first while.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:Carl3003
ID: 12098127
thanks, that worked...
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 12098407
fseek(fp, 0L, SEEK_SET);
while (fgets( line, LINELEN, fp ))  total_lines++;
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12098693
Have you solved your problem?
0
 

Author Comment

by:Carl3003
ID: 12098954
yes, i realized that i could reopen the file again but rewind(fp) worked for me as well..
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question