This C Program Collect Statistics of a Source File like Total Lines, Total no. of Blank Lines, Total no. of Lines ending with Semicolon.
Here is source code of the C Program to Collect Statistics of a Source File like Total Lines, Total no. of Blank Lines, Total no. of Lines ending with Semicolon. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* 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);
}
$ 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 Semicolon 6 The length of Actual code is 13
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Related Posts:
- Practice Computer Science MCQs
- Apply for C Internship
- Check C Books
- Check Computer Science Books
- Watch Advanced C Programming Videos