Hey guys, I'm having another problem (two actually) with a function that reads 5 variables (type int) stored in an text file, separated by lines (\n) and then it returns them to use in the main() program.
The thing is, I do not know how to return them to the main program, probably using a typedef struct, but I never figured out how those worked (lousy teachers!), so I'm stuck. My other problem is quite odd, I'm using the function fgets() to read the values and then sscanf() to change them to int type, but the third is printing out as -858993460 something (all other 4 numbers are displayed correctly).
Here's the code for that function:
void Read_Environment() // It's void because I don't know how to return them yet
{
char slight[4], spresence[2], stemperature[4], swind[4], shumidity[4]; // Strings, presence is either 0 or 1, all others vary from 0-100%
int light, presence, temperature, wind, humidity;
FILE *pf1 = fopen("environment.txt","r");
if(!pf1)
{
printf("ERROR: Problem in file opening!\n");
}
else // Read data
{
fgets(slight, 4, pf1);
sscanf(slight, "%d", &light);
fgets(spresence, 2, pf1);
sscanf(spresence, "%d", &presence);
fgets(stemperature, 4, pf1);
sscanf(stemperature, "%d", &temperature); // This one is printing -858993460 instead of the stored value
fgets(swind, 4, pf1);
sscanf(swind, "%d", &wind);
fgets(shumidity, 4, pf1);
sscanf(shumidity, "%d", &humidity);
printf("%d\n%d\n%d\n%d\n%d", light, presence, temperature, wind, humidity); // For test purposes
fclose(pf1);
}
}
Sample of environment.txt file:
5
1
5
5
5
Output when running code:
5
1
-858993460
5
5