Solved

returning number of lines in a file?

Posted on 2004-09-19
7
373 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now