Questions: Top-30-c-programs-asked-in-interview_6304.html
18.Write a program to find the number of lines in a text file.
Number of lines in a file can be determined by counting the number of new line characters present.
Program: Program to count number of lines in a file.
#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
Explanatio n:
In this program, name of the file to be read is taken as input. A file by the given name is opened in read-mode
using a File pointer'fp'. Characters from the file are read into a char variable'sample_ch r'with the help of getc
function. If a new line character( '\n') is encountere d, the integer variable'no_lines' is incremente d. If the
character read into'sample_ch ar'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 encountere d. The file pointer is then closed and the total
number of lines is shown as output.
18.Write a program to find the number of lines in a text file.
Number of lines in a file can be determined by counting the number of new line characters present.
Program: Program to count number of lines in a file.
#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
Explanatio n:
In this program, name of the file to be read is taken as input. A file by the given name is opened in read-mode
using a File pointer'fp'. Characters from the file are read into a char variable'sample_ch r'with the help of getc
function. If a new line character( '\n') is encountere d, the integer variable'no_lines' is incremente d. If the
character read into'sample_ch ar'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 encountere d. The file pointer is then closed and the total
number of lines is shown as output.
0 comments:
Post a Comment