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);            
}
 
maybe problem accessing or reading the "pInput" file...make sure you have formatted the file correctly (i.e. in notepad, choose save as and select different format), and if it has an extension, include that (i.e. pInput.txt)

just grasping at straws, but worth a try!

good luck
 
chucky said:
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);            
}

You're trying to do your fscanf call into an array that hasnt had any memory allocated to it.

Change the 'char str[] ="";' line to something like 'char str[50]'.

BTW - the 50 there is just a guideline - it should be high enough to fit enough characters in to accomodate the longest line you're likely to read.
 
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.
 
chucky said:
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.

One of the joys of C.


Did it solve the problem?
 
Back
Top Bottom