I'd like to teach the world to c
Printable View
I'd like to teach the world to c
Huh?
ehem, cough, sorry ahd some problems there with awriting a post thinking I was logged in, but turns out I wasn't, so I lost it.
Anyhow, keeping with the subject, I'd like to read in some variable values from a setting text file, which goes like so:
varname = value
so
fscanf(filein, "%s = %i", varname, &value);
would *roughly* be what I'd do ...
If the order of the variables names is observed, and known to the program the job is not too difficult. But if not - and it would be more robust - the program needs to recognise the varname is the correct. strcmp() would be the way here and then a conditional assigning.
Is there a more direct way? JUst looking for hints. I apologise for the poor wording.Please understand it's the second time I write this post.
Thanks in advance. Cheers.
So I take it that if it says "a = 0", then the variable a should be assigned 0?
If so, then I suggest you use a map. There is no standard map in C, however, so you may need to google a bit to find a good one.
Still not sure what you mean, need to explain by giving a clearer example.
I think what he means is if having a config file like for example
he wants to parse the file and use the rvalue in his code by looking up the lvalue. If so then any implementation of a linked list should do, and you could do something likeCode:Name = My Name
Age = 123
DecimalVar = 3.3
Code:struct Variable
{
const char* Name;
void* Data;
int VariableType;
struct Variable* next;
}
struct Variable* list = NULL;
ParseConfigFile("filename.txt", &list);
const char* name = GetStringVar(list, "Name");
int age = GetIntVar(list, "Age");
float floatVar = GetFloatVar(list, "DecimalVar");
SetStringVar(list, "NewStringVar", "Hello World!");
SaveConfigFile("filename.txt", list);