C programming problems

Associate
Joined
24 Mar 2004
Posts
893
Location
Northern Ireland
Im currently trying to make a program that will count various parameters of other c programs.

My program is fundementally flawed somehow. The program keeps re-entering the countWhiles() function for some reason, and then throughs up a Access violation.

Ive stripped everything else out of the program.

Does anyone know whats going wrong?

Code:
#include <stdio.h>
#include <string.h>

FILE *pInput; 

void countWhiles(void);

int main()
{

    /* open the file to be read */
    pInput = fopen("1.c", "r");  
    
    countWhiles();
   
   
    exit(0);
}

void countWhiles()
{
     char str[] ="";
     int count = 0;
     
     while (!feof(pInput))
     {
        fscanf (pInput, "%s", str);
		if (strcmp( str, "/*") == 0)
		{
			while(!(strcmp( str, "*/") == 0))
				fscanf (pInput, "%s", str);
		}
		else if(strcmp( str, "while" ) == 0)
		     count++;    
    } 
   rewind(pInput); 
	/* output the results */
	printf("The program contains: %d while statments\n",count);            
}
 
Change the 'char str[] ="";' line to something like 'char str[50]'.

Cheeers, cant believe it was something like that.

For some reason i had it in my head that if i left it blank it would just be as big as it needs to be.
 
Back
Top Bottom