well for parsing, you simply do this: (might be a bit slow, but this is easiest)
//some variables to be used later on
char c = 0;
int pos = 0;
int stage = 0;
char variable[200];
char value[200];
while ( 1 )
{
c = fgetc ( fp );//get the next char from the file
if ( c == EOF ) //end of file, break loop
break;
if ( c == ';') //end of command
{
value[pos] = '\0';//null terminator
pos = 0; //reset string position
stage = 0; //reset reading stage
//now variable and value strings contains the info you need for loading, for example, if you have this in the saved file :"ThisVar = 10;", now variable will have the string "ThisVar" and value will have "10", you can convert the "10" to an integer or float using atof and atoi
//TODO: add what you want to do with the info here
continue; //continue loop and search for other commands in the file
}
if ( c == '=' ) //if it's a '=', change stage
{
variable[pos] = '\0'; //add a null terminator (you need this at the end of a string to identify the "end of string")
stage = 1; //next stage (get the value of the variable)
pos = 0; //reset pos to start writing from element 0
}
if ( stage == 0 && c != ' ' /*ignore spaces*/ )
variable[pos++] = c;
else if ( stage == 1 && c != ' ' /*ignore spaces*/ )
value[pos++] = c;
}
what happens there is:
you loop forever untill EOF character is read, store everything you read in the variable "variable", once a '=' is reached, terminate "variable" (by adding a '\0' at its ending) and go to stage 1 which is reading to "value" variable, now, once ';' is reached, terminate "value" and then use the info you have, repeat this untill EOF is reached
notice that we won't read the spaces (" "), why? because it will cause problems on your side, for example, in the file you have "ThisVar = 10", it will be read like "ThisVar " and " 10", no problem with the 10 but your string comparison won't succeed assuming you will compare "ThisVar" to "ThisVar "
also, for comparing, use:
if ( strcmp ( variable, "ThisVar" ) == 0 )
{
//variable is contains "ThisVar"
}
simplest method i believe, can be optimized in many ways to suit what you want, hope this helps
Your signature has been erased by a mod - Please reduce it to 600x120 maximum size