Sorry if this is fairly obvious but I haven't been able to find it anywhere. I'm writing a config file parser in c. the files will look something along the lines of
PARAM1 VALUE1
PARAM2 VALUE2
etc. I'm using fscanf to read in the file and everything works smoothly but it needs to be able to toss out garbage such as random characters. It does this well unless the random characters have an uneven number f breaks (ie 3 words, such as "sdjsdh sddfi sifsi" or 1 word sch as "sjdasfh"). In which case it uses the param value from the next line down as a "value" related as the last word in the above line's "parameter". The code is smart enough to toss this out but then the next time fscanf gets called, it makes the first unused value the parameters, which should actually be a value(ie it thinks VALUE1 is the parameter and PARAM2 is the value). My fscanf command looks like this:
fscanf(fp, "%s %s",temp_param, temp_value);
Where temp_value is the value and temp_param is the parameter.
The value's type is determined and then the parameter is checked against a list of allowed parameters like so:
for(int y = 0; y< NUMBER_OF_PARAMS; y++){
if (strcmp(temp_param, parameters[y])==0){
found = 1;
return y;
And then a strucutre where each variable is associated with a parameters[y] value is assigned:
if (goodparam == 0){
test->something= int_convert;
}else if (goodparam == 1){
test->anotherthing = int_convert;
}else if (goodparam == 2){
test->thing3 = int_convert;
}else if (goodparam == 3){
test->thingy = int_convert;
}else if (goodparam == 4){
strcpy(test->fakestring,temp_value);
}else if (goodparam == 5){
strcpy(test->otherstring,temp_value);
Is there a way I can get fscanf to stop in its tracks if it hits a newline character so this isn't screwed up?
Thanks