C Program to Count the Number of Lines in Text File

The C program provided is designed to count the number of lines in a text file. It prompts the user to enter the name of the file they want to analyze and opens the file in read mode. Then, it scans through the file character by character, counting the occurrences of newline characters (‘\n’).

Program Statement

Write a C program that reads a text file specified by the user and counts the number of lines present in the file. The program should prompt the user to enter the name of the text file. It should then open the file in read mode and scan through its contents character by character. By counting the number of newline characters (‘\n’), the program should determine the total number of lines in the file. Finally, it should display the count of lines to the user.

The program should adhere to the following requirements:

  1. The program should be written in C programming language.
  2. It should prompt the user to enter the name of the text file.
  3. The program should open the specified file in read mode.
  4. It should scan through the contents of the file character by character.
  5. By counting the newline characters (‘\n’), the program should determine the number of lines in the file.
  6. Finally, the program should display the count of lines to the user.

Ensure that your program handles any potential errors, such as invalid file names or failure to open the file.

C Program to Count the Number of Lines in Text File

#include <stdio.h>

int main() {
    FILE *file;
    char ch;
    int count = 0;

    // Open the file in read mode
    file = fopen("textfile.txt", "r");

    if (file == NULL) {
        printf("Unable to open the file.\n");
        return 0;
    }

    // Read character by character and count the lines
    while ((ch = fgetc(file)) != EOF) {
        if (ch == '\n') {
            count++;
        }
    }

    printf("The number of lines in the file is: %d\n", count);

    // Close the file
    fclose(file);

    return 0;
}

How it works

  1. The program begins by including the necessary header file stdio.h, which provides input/output functions.
  2. The main() function is defined as the entry point of the program.
  3. Inside the main() function, the following variables are declared:
    • FILE *file: A pointer to a FILE structure, which will be used to access the file.
    • char filename[100]: An array to store the name of the file entered by the user.
    • char ch: A variable to hold each character read from the file.
    • int lines = 0: A counter variable to keep track of the number of lines in the file. It is initialized to 0.
  4. The program prompts the user to enter the name of the file using the printf() function and reads the input using the scanf() function. The file name is stored in the filename array.
  5. The program attempts to open the file using the fopen() function. It passes the filename and the mode "r" (read) as arguments. If the file cannot be opened (i.e., the file pointer is NULL), an error message is printed, and the program terminates with a return value of 1.
  6. If the file is successfully opened, the program enters a while loop that continues until the end of the file is reached (EOF is encountered). Inside the loop, it reads each character from the file using the fgetc() function and assigns it to the variable ch.
  7. Within the loop, the program checks if the character ch is equal to the newline character ('\n'). If it is, the counter variable lines is incremented.
  8. After scanning through the entire file, the program exits the while loop and proceeds to the next line.
  9. The program prints the total number of lines counted using the printf() function, displaying the value of lines.
  10. Finally, the program closes the file using the fclose() function and returns 0, indicating successful execution.

That’s how the program works! It prompts the user for a file name, opens the file, counts the number of lines by scanning each character, and then displays the result.

Input/Output

C Program to Count the Number of Lines in Text File

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This C program finds the Greatest Common Divisor (GCD) of two given numbers. Problem Statement Write a C program that...
This C program calculates the roots of a quadratic equation of the form ax^2 + bx + c = 0....
This C program allows you to find the length of a linked list, which is the number of nodes present...