I have this code that is supposed to read from file and look up the parameters and read the values. In short I pass at program startup the configuration file and the program should read the values to configure the system. But I don’t understand what is wrong and especially why it doesn’t read the values.
void read_configuration_file(const char* filename) {
FILE* file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "Error: Cannot open configuration file.n");
exit(EXIT_FAILURE);
}
char parameter[100];
int value;
while (fscanf(file, "%s %d", parameter, &value) == 2) {
//I inserted debug prints but it doesn't enter this while
if (strcmp(parameter, "ENERGY_DEMAND") == 0) {
energy_demand = value;
printf("energy_demand %dn", energy_demand);
} else if (strcmp(parameter, "SIM_DURATION") == 0) {
sim_duration = value;
printf("sim_duration %dn", sim_duration);
} else if (strcmp(parameter, "ENERGY_EXPLODE_THRESHOLD") == 0) {
energy_explode_threshold = value;
printf("energy_explode_threshold %dn", energy_explode_threshold);
}
}
fclose(file);
}
This is the configuration file from which I read
ENERGY_DEMAND = 1
SIM_DURATION = 1
ENERGY_EXPLODE_THRESHOLD = 1
How could I modify the code to make it work?