Program To Find The Number Of Lines In A Text File/Programming Tricks

Posted by Unknown Selasa, 21 Januari 2014 0 komentar
Here sharing a program to find no.of lines in a text file, as we know Number of lines in a file can be determined by counting the number of new line characters present.

#include
int main()
/* Ask for a filename and count number of lines in the file*/
{
//a pointer to a FILE structure
FILE *fp;
int no_lines = 0;
// consider 40 character string to store filename
char filename[4 0], sample_chr ;
//asks user for file name
printf("En ter file name:");
// receives file name from user and stores in a string named'filename'
scanf("%s" , filename);
//open file in read mode
fp = fopen(file name,"r");
//get character from file and store in sample_chr
sample_chr = getc(fp);
while (sample_ch r != EOF){
// Count whenever sample_chr is'\n'(new line) is encountere d
if (sample_ch r =='\n')
{
// increment variable'no_lines' by 1
no_lines=n o_lines+1;
}
//take next character from file.
sample_chr = getc(fp);
}
fclose(fp) ; //close file.
printf("Th ere are %d lines in %s \n", no_lines, filename);
return 0;
}

Output:
Enter file name:abc.t xt
There are 4 lines in abc.txt
 
NOTE:
In this program, name of the file to be read is taken as input. A file by the given name is opened in read-modeusing a File pointer 'fp'. Characters from the file are read into a char variable' sample_chr with the help of getc function. If a new line character( '\n') is encountered , the integer variable no_lines' is incremented. If the character read into sample_char is not a new line character, next character is read from the file. This process is continued until the last character of the file(EOF) is encountered. The file pointer is then closed and the total number of lines is shown as output
TERIMA KASIH ATAS KUNJUNGAN SAUDARA
Judul: Program To Find The Number Of Lines In A Text File/Programming Tricks
Ditulis oleh Unknown
Rating Blog 5 dari 5
Semoga artikel ini bermanfaat bagi saudara. Jika ingin mengutip, baik itu sebagian atau keseluruhan dari isi artikel ini harap menyertakan link dofollow ke https://androidjones7.blogspot.com/2014/01/program-to-find-number-of-lines-in-text.html. Terima kasih sudah singgah membaca artikel ini.

0 komentar:

Posting Komentar

Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android jones.