Solved

returning number of lines in a file?

Posted on 2004-09-19
7
381 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Statment to match two tables in c# 6 77
How do I install gcc 4.8.4 on a Linux Ubuntu 14.04 machine? 5 1,576
How to align numbers in C using the %d 2 89
Problem to show menu 5 87
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…
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…
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.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

895 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

12 Experts available now in Live!

Get 1:1 Help Now