Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

returning number of lines in a file?

Posted on 2004-09-19
7
Medium Priority
?
419 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 total points
ID: 12098061
call rewind(fp) after the first while.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

705 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