So I'm writing a function in my program which will, among other things, open a bunch of files and read data from them through a for loop. The file names are in the format "Lightcurves\modelB.##.mag" where ## refers to i, the variable I'm looping through, which goes from 0 to 90 in steps of 5.
Before I put in the loop with the fscanf, it worked fine and said it opened all the files properly, now it crashes when I run it. I'm not quite sure how to read the data now because that hasn't ever happened to me before.
After a bit of help with a particular problem in another thread, I've got this now:
Code:
FILE *model; //All the global declarations that I use in fitCurves(), I think.
char *modelname[];
char *mode = "r";
void fitCurves()
{
int i; //Inclination.
int modelsize; //Number of data points in model.
float phase[400]; //Phase of model.
float expmag[400]; //Expected magnitude of model.
char inc[3]; //Temporary string.
char *modelfile[19][20]; //Model file name.
char *temp; //Temporary string.
for (i = 0; i <= 90; i += 5) {
sprintf(inc, "%02d", i);
temp = malloc(strlen(modelname) + strlen(inc) + strlen("Lightcurves\\") + strlen(".mag") + 1);
sprintf(temp, "%s%s%s%s", "Lightcurves\\", modelname, inc, ".mag");
strcpy(modelfile[i/5], temp);
free(temp);
}
for (i = 10; i <= 90; i += 5) { //Loops through all inclinations.
model = openFile(modelfile[i/5]);
modelsize = 0;
while (!feof(model)) {
fscanf(model, "%f %f %f", &phase[modelsize], &expmag[modelsize]); //Scans file and reads data.
modelsize++;
}
modelsize--; //Readjusts size variable to account for final size++.
fclose(model);
} //Ends loop.
}
FILE* openFile(char *name)
{
FILE* file;
file = fopen(name, mode); //Open input file.
if (file == NULL) { //Error handling.
fprintf(stderr, "Can't open input file %s!\n", name);
//system("pause");
//exit(1);
} else {
printf("File %s opened successfully.\n", name);
}
return file;
}
Thanks so much for any help!!