Using flag for excluding double quotes...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Dragomir
    New Member
    • Jan 2011
    • 2

    #1

    Using flag for excluding double quotes...

    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?
    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",&current);
          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");
    }
    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.
  • tdlr
    New Member
    • Jan 2011
    • 22

    #2
    I think it isn't supposed to set states and then set it back immediately afterwards, because this is what it does:
    Code:
    else if (c=='"')
    	states=IN; /*here*/
    states is now true.
    Then, execution continues below at:
    Code:
    if (states==IN)
    		{ 
    			if (c=='"') /*and here*/
    				states=OUT; 
    		}
    states is still set to IN. It enters the block, c is still '"' and states is set back to OUT.

    Edit: I just tried it with a test file. If there's something like this in it:
    Code:
    string x = "foo = bar"
    the = between the quotes will not be counted, if you move the
    Code:
    if (states==IN)
    			{ 
    				if (c=='"') /*and here*/
    					states=OUT; 
    			}
    stuff before } one line above.
    Also: format your code better.

    Comment

    • Dragomir
      New Member
      • Jan 2011
      • 2

      #3
      Thank you for your tip, I came up with something different thought. I got rid of the initialization of the flag and it worked, sorry for the sloppy formating too.

      Comment

      Working...