- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C program to count characters, lines and number of words in a file
A file is a physical storage location on disk and a directory is a logical path which is used to organise the files. A file exists within a directory.
The three operations that we can perform on file are as follows −
- Open a file.
- Process file (read, write, modify).
- Save and close file.
Example
Consider an example given below −
- Open a file in write mode.
- Enter statements in the file.
The input file is as follows −
Hi welcome to my world This is C programming tutorial From tutorials Point
The output is as follows −
Number of characters = 72
Total words = 13
Total lines = 3
Program
Following is the C program to count characters, lines and number of words in a file −
#include <stdio.h> #include <stdlib.h> int main(){ FILE * file; char path[100]; char ch; int characters, words, lines; file=fopen("counting.txt","w"); printf("enter the text.press cntrl Z:
"); while((ch = getchar())!=EOF){ putc(ch,file); } fclose(file); printf("Enter source file path: "); scanf("%s", path); file = fopen(path, "r"); if (file == NULL){ printf("
Unable to open file.
"); exit(EXIT_FAILURE); } characters = words = lines = 0; while ((ch = fgetc(file)) != EOF){ characters++; if (ch == '
' || ch == '\0') lines++; if (ch == ' ' || ch == '\t' || ch == '
' || ch == '\0') words++; } if (characters > 0){ words++; lines++; } printf("
"); printf("Total characters = %d
", characters); printf("Total words = %d
", words); printf("Total lines = %d
", lines); fclose(file); return 0; }
Output
When the above program is executed, it produces the following result −
enter the text.press cntrl Z: Hi welcome to Tutorials Point C programming Articles Best tutorial In the world Try to have look on it All The Best ^Z Enter source file path: counting.txt Total characters = 116 Total words = 23 Total lines = 6
- Related Articles
- C# Program to count the number of lines in a file
- C Program to count the number of lines in a file?
- Linux WC Command Examples to Count Number of Lines, Words, Characters
- C# program to count the number of words in a string
- How to count the number of words in a text file using Java?
- How to count the number of lines in a text file using Java?
- Python Program to Calculate the Number of Words and the Number of Characters Present in a String
- Write a C program to find total number of lines in an existing file
- Java Program to count the number of words in a String
- How to count the total number of lines in the file in PowerShell?
- Python Program to Count Number of Lowercase Characters in a String
- Count no. of characters and words in a string in PL/SQL
- C++ program to print unique words in a file
- C++ program to count final number of characters are there after typing n characters in a crazy writer
- C# program to Count words in a given string

Advertisements