I have been pondering this task for awhile now and I just can't get it to work.
I am trying to get the function readParameters to read the text from a .txt file and transfer them to the specified variables. I have managed to eliminate almost all the warnings but I cannot figure out how to get it to transfer the values from the .txt file. So all I end up with is the program taking the second route that terminates the program as no parameters could be stored. And just to mention it I am working with C99.
The .txt file includes these values:
rows: 5
columns: 7
northTemp: 4.3
eastTemp: 3.1
southTemp: 2.7
westTemp: 0.9
limit: 0.00005
and the code that I have managed so far is this:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/*Function to transfer the parameters from the .txt file to the specified variables*/
int readParameters(FILE *infilep, int *rows, int *columns, double *northTemp,
double *eastTemp, double *southTemp,
double *westTemp, double *limit);
int main (int argc,char ** argv){
int rows, columns;
double northTemp, eastTemp, southTemp, westTemp, limit;
FILE *infilep;
int x;
/*Reads a filename from the commandline and open the file*/
infilep = fopen(argv[1], "r");
if(infilep == NULL) {
fprintf(stderr, "Couldn't open file %s\n", argv[1]);
return 1;
}
/*Reads the values from the file (not working yet though)*/
if (readParameters(infilep, &rows, &columns,
&northTemp, &eastTemp, &southTemp,
&westTemp ,&limit)== 1) {
printf("Could not read parameters.\n");
printf("Terminating program.\n");
return 0;
}
}
int readParameters(FILE *infilep, int *rows, int *columns, double *northTemp,
double *eastTemp, double *southTemp,
double *westTemp, double *limit) {
char storage[100];
char temp;
while (!feof(infilep)) {
if (fscanf(infilep, "%d\n", rows) == 1 &&
fscanf(infilep, "%d\n", columns) == 1 &&
fscanf(infilep, "%s: %lf\n", &temp, northTemp) == 2 &&
fscanf(infilep, "%s: %lf\n", &temp, eastTemp) == 2 &&
fscanf(infilep, "%s: %lf\n", &temp, southTemp) == 2 &&
fscanf(infilep, "%s: %lf\n", &temp, westTemp) == 2 &&
fscanf(infilep, "%s: %lf\n", &temp, limit) == 2)
{
printf("temp is %s, %d\n", &temp, *rows);
fclose(infilep);
return 0;
} else {
fclose(infilep);
printf("The file could not be opened.\n");
return 1;
}
}
}
Does anyone know what I am doing wrong here? Any criticism and help is welcomed.
Thanks in advance for your advice,
Mike