Users Online

· Guests Online: 47

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C Program to Find the Number of Lines in a Text File

Here is source code of the C Program to find the number of lines in a text file. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
* C Program to Find the Number of Lines in a Text File
*/
#include <stdio.h>

int main()
{
FILE *fileptr;
int count_lines = 0;
char filechar[40], chr;

printf("Enter file name: ");
scanf("%s", filechar);
fileptr = fopen(filechar, "r");
//extract character from file and store in chr
chr = getc(fileptr);
while (chr != EOF)
{
//Count whenever new line is encountered
if (chr == 'n')
{
count_lines = count_lines + 1;
}
//take next character from file.
chr = getc(fileptr);
}
fclose(fileptr); //close file.
printf("There are %d lines in %s in a file\n", count_lines, filechar);
return 0;
}



$ cc pgm49.c
$ a.out
Enter file name: pgm2.c
There are 43 lines in pgm2.c in a file

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.76 seconds
10,272,300 unique visits