What is the process to count the number of characters and lines in a file?

To get a quick summary of a file like total number of characters, words and limes, Linux already has a tool, wc. Here we’ll see how to write C program to get the similar information.

Strategy to Count Characters, Words, Lines in a File

  1. Take input of a file name and open that file in a read only mode. Don’t continue if the file can’t be opened.
  2. Traverse the file character by character until you get the EOF character. Every file ends with the EOF character.
    1. Increment the character count.
    2. If the character is not a white-space character, set a flag in_word to 1.
    3. If the character is a white-space and the in_word flag is 1, increment the word count and set the in_word flag to 0.
      1. If the character is either ‘\n’ or ‘\0’, increment the line count.

The Program

/*test.c*/

#include 
#define MAX_LEN 1024

int main() {
  /*Read the file.*/

  char ch;
  int char_count = 0, word_count = 0, line_count = 0;
  int in_word = 0;
  char file_name[MAX_LEN];
  FILE *fp;

  printf("Enter a file name: ");
  scanf("%s", file_name);

  fp = fopen(file_name, "r");

  if(fp == NULL) {
    printf("Could not open the file %s\n", file_name);
    return 1;
  }

  while ((ch = fgetc(fp)) != EOF) {
    char_count++;

    if(ch == ' ' || ch == '\t' || ch == '\0' || ch == '\n') {
      if (in_word) {
        in_word = 0;
        word_count++;
      }

      if(ch = '\0' || ch == '\n') line_count++;

    } else {
      in_word = 1;
    }
  }

  printf("In the file %s:\n", file_name);
  printf("Number of characters: %d.\n", char_count);
  printf("Number of words: %d.\n", word_count);
  printf("Number of lines: %d.\n", line_count);

  return 0;
}

Here is the content of out sample text file (test.txt).

Electric communication will never
be a substitute for the face of
someone who with their soul encourages
another person to be brave and true

And here is the output of the program.

What is the process to count the number of characters and lines in a file?

Here character count includes all characters including white spaces. Word is consecutive non-white-space characters. And line ends with ‘\0’ or ‘\n’ character.

What is the process to count the number of characters and lines in a file?

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation. View all posts by Srikanta

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 −

 Live Demo

#include 
#include 
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

What is the process to count the number of characters and lines in a file?


What is the process to count the number of characters and lines in a file?

What is the process to count the number of characters and lines in a file in Unix?

wc stands for word count. As the name implies, it is mainly used for counting purpose. It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments.

Which command counts lines and characters in a file?

Use the wc command to count the number of lines, words, and bytes in the files specified by the File parameter. If a file is not specified for the File parameter, standard input is used. The command writes the results to standard output and keeps a total count for all named files.

How to count lines in file C?

C Program to Count the Number of Lines in Text File.
* C Program to Find the Number of Lines in a Text File..
#include .
int main().
FILE *fileptr;.
int count_lines = 0;.
char filechar[40], chr;.

What is the command to count only the number of lines in the file text file?

The command “wc” basically means “word count” and with different optional parameters one can use it to count the number of lines, words, and characters in a text file.