Created
May 8, 2016 08:26
-
-
Save complxalgorithm/fddfbac09b976a4dc0e202f17c2a9c4f to your computer and use it in GitHub Desktop.
C program that will collect statistics of a source file, e.g., total lines, total number of blank lines, total number of lines ending with a semicolon, etc. Example:
$ cc file8.c
$ a.out lines.c The Total number of lines are 23
The Total number of Commented lines are 6
The Total number of blank lines are 4
The total number of lines that end with…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* C Program to Collect Statistics of a Source File like Total Lines, | |
* Total no. of Blank Lines, Total no. of Lines ending with Semicolon | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
void main(int argc, char *argv[]) /* Command line Arguments */ | |
{ | |
int ncount = 0, ccount = 0, scount = 0, blank = 0; | |
char ch; | |
FILE *fp; | |
fp = fopen(argv[1], "r"); | |
if (fp == NULL) | |
{ | |
perror("Error Occured"); | |
} | |
else | |
{ | |
while(1) | |
{ | |
ch = fgetc(fp); | |
if (ch == EOF) | |
{ | |
break; | |
} | |
if (ch == 10) | |
{ | |
ncount++; | |
if (ch = fgetc(fp) == '\n') | |
{ | |
fseek(fp, -1, 1); /* shifting offset of the file to previous position */ | |
blank++; | |
} | |
} | |
else if (ch == 59) | |
{ | |
scount++; | |
} | |
else if (ch == '/' || ch == '*') | |
{ | |
ccount++; | |
} | |
} | |
} | |
printf("\nThe Total number of lines are %d", ncount); | |
printf("\nThe Total number of Commented lines are %d", ccount); | |
printf("\nThe Total number of blank lines are %d", blank); | |
printf("\nThe total number of lines that end with Semicolon %d", scount); | |
printf("\nThe length of Actual code is %d ", ncount-blank-ccount); | |
fclose(fp); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment