Users Online
· Guests Online: 117
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
This C Program Collect Statistics of a Source File like Total Li
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
/*
* 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
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.