Hello,
This is my first program in C, so please don't be too critical
I have a config file parser I wrote that is really causing some problems. I had it working and literally all-of-a-sudden it stopped working!
Here is the code:
File: program_definitions.cfg
File: config_parser.h
And I have a program that loads config_parser.h and calls the config_parser() method and passes in the name of the config file.
When I compile and then attempt to execute the program, I get a core dump and the following from the debugger:
Line 53 is:
Any help would be appreciated!
ICQ: 54380631
This is my first program in C, so please don't be too critical
I have a config file parser I wrote that is really causing some problems. I had it working and literally all-of-a-sudden it stopped working!
Here is the code:
File: program_definitions.cfg
Code:
SCRIPT_NAME=Currency Conversion
CONVERT_TO_LABEL=US Dollars
STRING_DIVIDER=----------------------------------------
File: config_parser.h
Code:
#include <iostream>
#include <string>
#include <ctype.h>
int config_parser(char *fileName);
char *TrimSpaces(char *str);
char *SCRIPT_NAME;
char *STRING_DIVIDER;
char *CONVERT_TO_LABEL;
int config_parser(char *fileName)
{
FILE *filePtr;
if( (filePtr = fopen( fileName, "r" )) == NULL )
{
printf("%s -- Unable to open configuration file.%s","Error!",fileName);
exit( 1 );
}
else
{
while( !feof(filePtr) )
{
char data[255];
char *pt;
char *tmp_tok_ptr;
fgets(data, 255, filePtr);
//printf("data = %s\n",data);
if( (data[0] != '\n') && (data[0] != '#') )
{
//printf("data[0] = [%c]\n",data[0]);
pt = strtok(data, "=");
printf("pt = [%s]\n",pt);
if(strcmp(pt,"SCRIPT_NAME") == 0)
{
tmp_tok_ptr = strtok(NULL, "=");
SCRIPT_NAME = (char *) calloc(strlen(tmp_tok_ptr), sizeof(char));
strncpy(SCRIPT_NAME,tmp_tok_ptr,strlen(tmp_tok_ptr));
SCRIPT_NAME = TrimSpaces(SCRIPT_NAME);
//printf("SCRIPT_NAME = [%s]\n",SCRIPT_NAME);
}
else if(strcmp(pt,"STRING_DIVIDER") == 0)
{
tmp_tok_ptr = strtok(NULL, "=");
STRING_DIVIDER = (char *) calloc(strlen(tmp_tok_ptr), sizeof(char));
strncpy(STRING_DIVIDER,tmp_tok_ptr,strlen(tmp_tok_ptr));
STRING_DIVIDER = TrimSpaces(STRING_DIVIDER);
//printf("STRING_DIVIDER = [%s]\n",STRING_DIVIDER);
}
else if(strcmp(pt,"CONVERT_TO_LABEL") == 0)
{
tmp_tok_ptr = strtok(NULL, "=");
CONVERT_TO_LABEL = (char *) calloc(strlen(tmp_tok_ptr), sizeof(char));
strncpy(CONVERT_TO_LABEL,tmp_tok_ptr,strlen(tmp_tok_ptr));
CONVERT_TO_LABEL = TrimSpaces(CONVERT_TO_LABEL);
//printf("CONVERT_TO_LABEL = [%s]\n",CONVERT_TO_LABEL);
}
}
}
fclose(filePtr);
}
return 0;
}
/**
* Removes left and right spaces from a string
*
* @param str String to trim
*
* @return char* to the trimed string
*/
char *TrimSpaces(char *str)
{
size_t len;
char *right, *left;
/* Trim whitespace from left side */
for (left = str; isspace(*left); left++);
/* Trim whitespace from right side */
if ((len = strlen(left)))
{
right = left + (len - 1);
while (isspace(*right))
{
*right = '\0';
right--;
}
}
/* Only do the str copy if their was spaces to the left */
if (left != str)
strcpy(str, left);
return (str);
}
And I have a program that loads config_parser.h and calls the config_parser() method and passes in the name of the config file.
When I compile and then attempt to execute the program, I get a core dump and the following from the debugger:
Code:
> currency_conversion.exe!config_parser(char * fileName=0x0040a298) Line 53 + 0x8 C++
currency_conversion.exe!main(int argc=1145656905, char * * argv=0x2d005245) Line 77 C++
currency_conversion.exe!mainCRTStartup() Line 259 + 0x12 C
kernel32.dll!77e814c7()
Line 53 is:
Code:
STRING_DIVIDER = (char *) calloc(strlen(tmp_tok_ptr), sizeof(char));
Any help would be appreciated!
ICQ: 54380631