I am trying to create a program which, given an input file, returns the count of all the lines of code in the input file, excluding blank lines and comment lines. I have written the following code, however I need help with how to exclude lines containing comments and blank lines.
#include<stdio.h>
int main()
{
int count;
char ch;
FILE *fptr;
clrscr();
fp=fopen("test.cpp","r");
if(fp==EOF)
{
perror("Error:");
}
else
{
while(ch!=EOF)
{
ch=fgetc(fptr);
if(ch=='\n')
count++;
if(ch=='\\')
count--;
if(ch=='\*')
{
while(ch!='*\')
{
ch=fgetc(fptr);
}
}
}
printf("the lines in the code are %d\n",count);
fclose (fptr)
}
getchar();
return 0;
}
How can I modify the above code so that blank lines and comment lines are not counted?