473,382 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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?
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #define IN (1)
  3. #define OUT (0)
  4. int main()
  5. {
  6.     FILE *fp;
  7.     char c;
  8.     char d;
  9.     int m;
  10.     int i;
  11.     char f_name[255];
  12.     char current[70],ch=' ';
  13.     char type[][15]={"if","for","while","go","break;","continue","switch"};
  14.     int brspace=0;
  15.     int brp=0;
  16.     int brop=0;
  17.     int state=OUT; /*this is my flag for comments*/
  18.     int states=OUT; /*this is my flag for double quotes*/
  19.  
  20.   printf("Name of file to read: ");
  21.   scanf("%s",f_name);
  22.   if((fp=fopen(f_name,"r"))==NULL)
  23.     {
  24.      printf("Error opening file!\n");
  25.      system ("pause");
  26.      exit (1);
  27.     }
  28.     while ((m=fscanf(fp, "%c", &c))!=EOF)
  29.     {
  30.            if (m==0)
  31.           {
  32.            printf("Invalid file!\n");
  33.            system ("pause");
  34.            exit (1); 
  35.           }
  36.  
  37.            if (c == '/')
  38.              {
  39.          d = getc(fp);
  40.              if(d=='*'||d=='/') 
  41.              state=IN;
  42.           }
  43.           else if (c=='"')
  44.           states=IN; /*here*/
  45.       if(state==IN)
  46.              {
  47.             if (c=='*')
  48.             {
  49.                 d = getc(fp);
  50.             if(d=='/')
  51.                 state=OUT;
  52.             }
  53.             if (c=='\n')
  54.             state=OUT;
  55.           }
  56.             if (states==IN)
  57.           { 
  58.             if (c=='"') /*and here*/
  59.             states=OUT; 
  60.           }              
  61.             if (state!=IN) 
  62.              {
  63.          if(c==' ')
  64.              brspace++;
  65.            }          
  66.            if (state==OUT&&states==OUT) /* And this is where I try */
  67.            {              
  68.             if (c=='=')
  69.             {
  70.              d=getc(fp);
  71.              if (d=='='||d=='\'')
  72.              continue;
  73.              else brp++;
  74.             }       
  75.            }        
  76.     }
  77.     rewind(fp);
  78.     while (!feof(fp))
  79.     {
  80.       fscanf(fp,"%s",&current);
  81.       for (i=0;i<10;i++)
  82.       if (!strcmp(current,type[i])) 
  83.       brop++;
  84.     } 
  85. fclose(fp);
  86. printf("spaces in comments %d\n",brspace);
  87. printf("= symbols %d\n", brp);
  88. printf("operators %d\n", brop);
  89. system("pause");
  90. }
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.
Jan 3 '11 #1
2 1857
tdlr
22
I think it isn't supposed to set states and then set it back immediately afterwards, because this is what it does:
Expand|Select|Wrap|Line Numbers
  1. else if (c=='"')
  2.     states=IN; /*here*/
states is now true.
Then, execution continues below at:
Expand|Select|Wrap|Line Numbers
  1. if (states==IN)
  2.         { 
  3.             if (c=='"') /*and here*/
  4.                 states=OUT; 
  5.         }    
  6.  
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:
Expand|Select|Wrap|Line Numbers
  1. string x = "foo = bar"
the = between the quotes will not be counted, if you move the
Expand|Select|Wrap|Line Numbers
  1. if (states==IN)
  2.             { 
  3.                 if (c=='"') /*and here*/
  4.                     states=OUT; 
  5.             }  
stuff before } one line above.
Also: format your code better.
Jan 3 '11 #2
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.
Jan 4 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
2
by: girish | last post by:
In my XML document, some node attributes data contains both single quot and double quote characters, such as <input msg="Hello "World", What's up"/>. The double quotes are in form of escape...
5
by: Joel | last post by:
Hi, I incorporated a function in my code that whenever I use a string variable in an sql statement if the string contains a single quote it will encase it in double quotes else single quotes. ...
24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
4
by: (PeteCresswell) | last post by:
Is his just a flat-out "No-No" or is there some workaround when it comes time for SQL searches and DAO.FindFirsts against fields containing same? I can see maybe wrapping the value searched for...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
16
by: Charles Law | last post by:
I have a string similar to the following: " MyString 40 "Hello world" all " It contains white space that may be spaces or tabs, or a combination, and I want to produce an array...
1
by: desi.american | last post by:
I have a dynamically generates ASPX page with tables and data. Depending on user selection, the same page can be viewed as a simple web page (rendered in HTML) or as an excel spreadsheet. If the...
4
by: Justin Fancy | last post by:
Hi everyone, I need to replace all instances of a double quote(") with two single quotes('') in a text file. I already have some replacements of strings going on, but I tried this one, but the...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.