Hi, I'm using a flag to try and not include text for parsing in double quotes.
When trying to use flag for comments it works but it doesn't with the double quotes...
Can you tell me why it didn't work?
I'm interested how to exclude the text in the double quotes from being counted. I'm reading from a C source-code file as text.
When trying to use flag for comments it works but it doesn't with the double quotes...
Can you tell me why it didn't work?
Code:
#include <stdio.h>
#define IN (1)
#define OUT (0)
int main()
{
FILE *fp;
char c;
char d;
int m;
int i;
char f_name[255];
char current[70],ch=' ';
char type[][15]={"if","for","while","go","break;","continue","switch"};
int brspace=0;
int brp=0;
int brop=0;
int state=OUT; /*this is my flag for comments*/
int states=OUT; /*this is my flag for double quotes*/
printf("Name of file to read: ");
scanf("%s",f_name);
if((fp=fopen(f_name,"r"))==NULL)
{
printf("Error opening file!\n");
system ("pause");
exit (1);
}
while ((m=fscanf(fp, "%c", &c))!=EOF)
{
if (m==0)
{
printf("Invalid file!\n");
system ("pause");
exit (1);
}
if (c == '/')
{
d = getc(fp);
if(d=='*'||d=='/')
state=IN;
}
else if (c=='"')
states=IN; /*here*/
if(state==IN)
{
if (c=='*')
{
d = getc(fp);
if(d=='/')
state=OUT;
}
if (c=='\n')
state=OUT;
}
if (states==IN)
{
if (c=='"') /*and here*/
states=OUT;
}
if (state!=IN)
{
if(c==' ')
brspace++;
}
if (state==OUT&&states==OUT) /* And this is where I try */
{
if (c=='=')
{
d=getc(fp);
if (d=='='||d=='\'')
continue;
else brp++;
}
}
}
rewind(fp);
while (!feof(fp))
{
fscanf(fp,"%s",¤t);
for (i=0;i<10;i++)
if (!strcmp(current,type[i]))
brop++;
}
fclose(fp);
printf("spaces in comments %d\n",brspace);
printf("= symbols %d\n", brp);
printf("operators %d\n", brop);
system("pause");
}
Comment