C program to count the number of lines, words, and characters in any text file
File :
A file is a specific physical storage location on disk, and a directory is a logical structure used to organize one or more files. There is a file in the directory.
In this tutorial example, we will learn to count the number of lines, characters and words in a text file using a C program.
Assume that a file named personal.txt contains the following data content:
Hi My name is Sankalan Data Technologies.
I am informative content writer, software developer
and corporate trainer.
A C program that shows how to count the number of lines, words, and characters in any text file:
A C program that shows how to count the number of lines, words, and characters in any text file:
#include <stdio.h>
int main()
{
FILE *fptr;
int words = 0, char_count= 0, lines = 0;
char file_name[100];
char ch;
printf("\nEnter the name of any text file: ");
scanf("%s", file_name);
fptr = fopen(file_name, "r");
if (fptr == NULL)
{
printf("Error while opening the text file\n");
return 1;
}
while ((ch = fgetc(fptr)) != EOF)
{
char_count++;
if (ch == '\n')
{
lines++;
}
if (ch == ' ' || ch == '\t'|| ch == '\n')
{
words++;
}
}
/* if file does not end with newline char (\n) count last word and line.*/
if (char_count > 0)
{
words++;
lines++;
}
fclose(fptr);
printf("\n Number of characters in text file is: %d ", char_count);
printf("\n Number of words in text file is: %d ", words);
printf("\n Number of lines in text file is: %d", lines);
return 0;
}
Explanation:
header file Section:
#include<stdio.h> is the first statement It contains the standard input/output library file, which provides standard library functions such as fopen, fgetc, fclose, scanf and printf used for file manipulation and input/output.
Variable declaration section:
fptr: is the file pointer that holds the reference(or address) to the opened file.
ch: A character variable is used to store the character read from the file.
o char_count: An integer variable,is used to keep track of the number of characters counted in a file.
o lines: An integer variable to keep track of the number of lines in a text file.
o words: An integer variable to keep track of the number of words.
o file_name: file_name: is the name of a file which consist of group of or a character array to store the name of the file .
Get file name:
The program prompts the user to enter the name of the file using printf and stores it in the file_name array using scanf.
Open file:
The fopen() is standard file function which opens the file specified by the file_name in read mode ("r"). The returns NULL if the file cannot be opened fails to open, and then display an error message.
Read characters:
The while loop executes indefinitely or until fgetc() reaches the end of file (EOF).
Inside the loop: The char_count value increases by one after each character is read.
o If the current character is a newline, space, or tab, the variable word is incremented by one to calculate the new word.
o Similarly, if the current character is a newline, such as \n, then the line number is incremented by one i.e lines is incremented by one .
At the last:
After the loop ends, the program checks if any characters have been read (char_count > 0). If so, counts or increment lines and words at the end if the file does not end with a newline.
Close file: The function fclose() is used to close the specific an opened file.
Output :
The C program uses the printf() format function to display the total number of words, characters, and lines.
Enter the name of any text file: personal.txt
Number of characters in text file is: 80
Number of words in text file is: 17
Number of lines in text file is: 3
The program correctly counts the characters, words, and lines in the file.
Previous Topic:-->> Binary Files in C. || Next topic:-->>Copy file in C.