Hi guys.
I'm having a problem with the lecture of a FILE in Devc++.
The file is:
F 5
F 10
F 17
B 23 2
F 42
B 50 5
C 70
F 90
The code I'm using to read the FILE is:
Code:
void assignaEspecial(char cMode, stCasella casella[97]){
FILE *f;
char strFitxer[50];
stFitxer fitxer[97];
int i = 0;
int proba = 5;
if(cMode == 'F' ||cMode == 'f'){
printf("Nom del fitxer: "); //File name
scanf("%s",strFitxer);
f = fopen(strFitxer,"r");
if( f == NULL){
printf("Error! Fitxer NULL\n");
}
else{ //Entra
printf("Hello?!"); //This printf appears in the screen
while(!feof(f)){ //Here crashes
fscanf(f,"%c",fitxer[i].cTipus);
fscanf(f,"%d", fitxer[i].nCasella);
printf("%d\n",fitxer[i].nCasella);
if(fitxer[i].cTipus == 'B'){
fscanf(f,"%d",fitxer[i].nTorns);
}
i++;
}
}
assignaFitxer(fitxer, casella, i);
fclose(f);
}
if(cMode == 'E' || cMode == 'e'){
printf("HE ENTRADO EN E\n");
for(i = 0; i < 96; i++){
if(i == 59 || i == 80){
casella[i].nEspecial = 2;
casella[i].nColor = 6;
}
if( i == 66 || i == 90){
casella[i].nEspecial = 3;
casella[i].nColor = 0;
}
if( i == 5 || i == 10 || i == 17 || i == 23 || i == 30 || i == 38 || i == 42 || i == 50 || i == 56 || i == 64 || i == 70 || i == 73 || i == 77 || i == 82 || i == 92){
casella[i].nEspecial = 1;
casella[i].nColor = 7;
}
}
}
}
When executing, appears the printf: "Hello?!" but then, the program crashes.
In another thread, anduril462 told me that using !feof(f) is not a good idea, but this is the only way to know when the file ends that our teachers showed us in the university, so I want to use it if it's possible.