C Program to Count Characters, Words and Lines in a Text File
A C program can read a text file and count the total number of characters, words and lines present in it. This program uses the fgetc() function to read the file character by character and counts characters, words and lines while displaying the file contents.
सी लैंग्वेज में प्रोग्राम द्वारा किसी टेक्स्ट फाइल को पढ़कर उसमें उपस्थित कुल Characters, Words और Lines की संख्या गिन सकते हैं। यह प्रोग्राम fgetc() फंक्शन की सहायता से फाइल को एक-एक कैरेक्टर पढ़ता है और फाइल का कंटेंट प्रदर्शित करते हुए Characters, Words और Lines की गणना करता है।
C Program
#include <stdio.h>
#include <ctype.h>
int main(void)
{
FILE *fp;
int ch;
int cc = 0, wc = 0, lc = 0;
int in_word = 0;
int line_has_char = 0;
fp = fopen("abc.txt", "r");
if (fp == NULL)
{
printf("Cannot open the file.\n");
return 1;
}
printf("Contents of the file:\n\n");
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
cc++;
if (ch == '\n')
{
lc++;
line_has_char = 0;
}
else
{
line_has_char = 1;
}
if (isspace((unsigned char)ch))
{
in_word = 0;
}
else if (in_word == 0)
{
wc++;
in_word = 1;
}
}
/* Count the last line if it has no newline */
if (line_has_char)
{
lc++;
}
if (ferror(fp))
{
printf("\nError while reading the file.\n");
fclose(fp);
return 1;
}
printf("\n\nNumber of characters = %d", cc);
printf("\nNumber of words = %d", wc);
printf("\nNumber of lines = %d\n", lc);
fclose(fp);
return 0;
}Sample Text File: abc.txt
First, create a text file named abc.txt and write the following contents in it.
सबसे पहले abc.txt नाम की एक टेक्स्ट फाइल बनाएं और उसमें निम्नलिखित कंटेंट लिखें।
C Programming is easy.
File handling in C.Save the file in the program's working directory.
प्रोग्राम की Working Directory में इस फाइल को सेव करें।
Output
Contents of the file:
C Programming is easy.
File handling in C.
Number of characters = 43
Number of words = 8
Number of lines = 2Explanation
fopen("abc.txt", "r")opens the text file in read mode. यदि फाइल उपलब्ध नहीं है, तोfopen()NULL return करता है।fgetc(fp)reads one character at a time from the file. यह फंक्शन फाइल से एक बार में एक कैरेक्टर पढ़ता है।cccounts the total number of characters, including spaces and newline characters. यह Spaces और Newline Characters सहित कुल कैरेक्टर्स की गणना करता है।isspace()checks whether a character is whitespace, such as a space, tab or newline. इसकी सहायता से प्रोग्राम यह पहचानता है कि एक Word कहां समाप्त होता है।wccounts the total number of words. यह लगातार आने वाले Non-Whitespace Characters को एक Word मानकर उनकी गणना करता है।lccounts the total number of lines. प्रत्येक Newline Character के लिए लाइन की गणना की जाती है। यदि अंतिम लाइन में Newline नहीं है, तो उसे भी गिना जाता है।fclose(fp)closes the opened file. यह फाइल को बंद करता है।
The file abc.txt must be saved in the program's current working directory. If you are using Turbo C, for example, this may be the BIN folder, depending on how you run the program. For other compilers and IDEs, the working directory may be different.
abc.txt फाइल को प्रोग्राम की Current Working Directory में सेव करना आवश्यक है। उदाहरण के लिए, Turbo C में यह BIN फोल्डर हो सकता है, लेकिन अन्य Compilers और IDEs में Working Directory अलग हो सकती है।
Note: This program counts characters as values returned by fgetc() from the text stream. For ordinary English text, this gives the expected character count. In UTF-8 files containing Hindi or other multibyte text, the count may represent bytes rather than visible characters.
Comments
Post a Comment