I want to be able to write some variable values in a text file and then read it from inside the application.
1.
Text file example :
window=0
screenwidth=800
screenheight=600
app_title=my project
So i want to read "window" and then its value, and then apply that value to appropriate variable inside the code.
Also I want to be able to read string or char values.
2.
I would also want to put more variables in the same row, and the read it one by one: 1 0 day month 1000 500
Actually I wouldn't go that route (manually parsing using streams) for a configuration file. Find an existing configuration file library with a syntax that you like and just use that. INI files are strangely popular and so are XML files. There's also boost::program_options for a possibility.
If you are going to do it that way (roll your own), I think the basic process is:
1) read the line
2) find offset of '='
3) read before the '=' into a token variable
4) read behind the'=' into a value variable
5) switch on the token to assign the value variable to the correct program variable.
6) repeat with next line
well, you could simply use
while (fgets (string , 300 , fp)!=NULL)
{
sscanf(string,"width %d",Width);
sscanf(string,"height %d",Height);
sscanf(string,"fullscreen %d",Fullscreen);
}
If the string is not matching, the variables won't be affected, so it can be simple as that. You can even check the return value of sscanf (look it up)
But that's C, not C++. I'm sure C++ has better methods
Here is what I have been using for awhile, it doesn't read everything at once, but you can continually check the file for various settings.
Feel free to use it or make one yourself based on this, Its a modified version from some tutorial I read about 5 years ago, sorry but I do not have the link :/
ReadConfig.h
//Read a specific variable from our config file and return its value
//This is our error logging class
#ifndef READCONFIG_OBJECT
int ReadConfigInt(char* Filename,char* Variable); //return -1 on fail, -2 on file not found
string ReadConfigString(const char* Filename,char* Variable); //returns NULL on failure
int ReadConfigInt(char* Filename,char* Variable)
{
char line[MAX_STRING];
memset(line,0,MAX_STRING);
ifstream myconfig(Filename);
if(!myconfig)//could not open file
{
return NULL; //-2 file not found
}
while(!myconfig.eof())
{
myconfig.getline(line, MAX_STRING); // delim defaults to '\n'
if(myconfig)
{
if(line[0] == '/') //we are skipping this line
{ // skip comment line
continue;
}
else
{
char *pointerchar = NULL;
char var[MAX_STRING] = {'\0'}; // variable name
char val[MAX_STRING] = {'\0'}; // value its set to
size_t length =0;
pointerchar = strchr(line, '\n'); // find our newline
if(pointerchar != NULL)
{
*pointerchar = '\0'; //change newline to null character
}
length = strlen(line);
pointerchar = strchr(line, '='); //find our equals character
if(pointerchar == NULL)
{
return NULL; //failure
} //Parse Error
memcpy(var, line, pointerchar - line);// get the part before the first =
pointerchar++;// skip the actual '=' sign
memcpy(val, pointerchar, (line+length)-pointerchar);// get the value of the var
if (strcmp(var,Variable)==0)//see if we found it
{
return atoi(val);
}
}
char line[MAX_STRING];
memset(line,0,MAX_STRING);
ifstream myconfig(Filename);
if(!myconfig)//could not open file
{
return "Unknown";
}
while(!myconfig.eof())
{
myconfig.getline(line, MAX_STRING); // delim defaults to '\n'
if(myconfig)
{
if(line[0] == '/') //we are skipping this line
{ // skip comment line
continue;
}
else
{
char *pointerchar = NULL;
char var[MAX_STRING] = {'\0'}; // variable name
char val[MAX_STRING] = {'\0'}; // value its set to
size_t length =0;
pointerchar = strchr(line, '\n'); // find our newline
if(pointerchar != NULL)
{
*pointerchar = '\0'; //change newline to null character
}
length = strlen(line);
pointerchar = strchr(line, '='); //find our equals character
if(pointerchar == NULL)
{
return NULL;
} //Parse Error
memcpy(var, line, pointerchar - line);// get the part before the first =
pointerchar++;// skip the actual '=' sign
memcpy(val, pointerchar, (line+length)-pointerchar);// get the value of the var
if (strcmp(var,Variable)==0)//see if we found it
{
return val;
}
}