Write a C Program to Count Lines,Blank Lines,Comments in File

By | 01.12.2016

Count Lines Blank Lines Comments in file


Write a C Program to Count Lines, Blank Lines, Comments in File. Here’s simple Program to Count Lines, Blank Lines, Comments in File in C Programming Language.


Below is the source code for C Program to Count Lines, Blank Lines, Comments in File which is successfully compiled and run on Windows System to produce desired output as shown below. Here, file4.txt contains the code of this program in notepad.


SOURCE CODE : :


/*  C Program to Count Lines, Blank Lines, Comments in File  */

#include <stdio.h>

int main()
{
    int line_count = 0, n_o_c_l = 0, n_o_n_b_l = 0, n_o_b_l = 0, n_e_c = 0;
    FILE *fp1;
    char ch;
    char * arrr = "C:\\Users\\acer\\Documents\\file4.txt";
    fp1 = fopen(arrr, "r");

    while ((ch = fgetc(fp1))!= EOF)
    {
        if (ch  ==  '\n')
        {
            line_count++;
        }
        if (ch  ==  '\n')
        {
            if ((ch = fgetc(fp1))  ==  '\n')
            {
                fseek(fp1, -1, 1);
                n_o_b_l++;
            }
        }
        if (ch  ==  ';')
        {
            if ((ch = fgetc(fp1))  ==  '\n')
            {
                fseek(fp1, -1, 1);
                n_e_c++;
            }
        }
    }
    fseek(fp1, 0, 0);
    while ((ch = fgetc(fp1))!= EOF)
    {
        if (ch  ==  '/')
        {
            if ((ch = fgetc(fp1))  ==  '/')
            {
                n_o_c_l++;
            }
        }
    }
    printf("Total no of lines: %d\n", line_count);
    printf("Total no of comment line: %d\n", n_o_c_l);
    printf("Total no of blank lines: %d\n", n_o_b_l);
    printf("Total no of non blank lines: %d\n", line_count-n_o_b_l);
    printf("Total no of lines end with semicolon: %d\n", n_e_c);

    return 0;
}

OUTPUT : :


Total no of lines: 54
Total no of comment line: 0
Total no of blank lines: 4
Total no of non blank lines: 50
Total no of lines end with semicolon: 18

Above is the source code for C Program to Count Lines, Blank Lines, Comments in File which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

1 1 vote
Article Rating
Category: C Programming File Handling Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments