Okay. The function now is able to read a line from config, and return it to the caller.
The problem now, is that it always returns the first line of configuration.
The config file looks like this:
Code:
test = abc
something = somethingelse
this = that
I request a line and print it out with:
Code:
char *blah = getconf(true,"something");
For some reason, getconf always returns "abc" -- the first line of the config file -- when it should be equaling "somethingelse", because it's something's value.
Here is the code I'm working with:
Code:
int main()
{
//This isn't the full main function, but it's the part I'm working with
char *blah = getconf(true,"something");
printf("%s\n",blah);
return(0);
}
char *getconf(bool writelog,char get[])
{
char line[256];
FILE *config;
int a = 0;
char setting[25];
char val[150];
config = fopen("test.conf","r");
while(fgets(line,sizeof(line),config) != NULL) {
a++;
if(line[0] == '#') continue; //If the line is a comment, skip it
if(sscanf(line,"%s = %s",setting,val) != 2) {
if(writelog == true) {
mlog = fopen("test.log","a");
fprintf(mlog,"%s config: %i: syntax error!\n",gettime(),a);
fclose(mlog);
}
continue;
}
if(strcmp(get,setting)) {
char *value = (char*)malloc(strlen(val) * sizeof(char));
strcpy(value,val);
return(value);
}
}
if(writelog == true) {
mlog = fopen("test.log","a");
fprintf(mlog,"%s config: %s: value not found!\n",gettime(),get);
fclose(mlog);
}
return("no value");
}
Thanks again!