0
I have data stored in a text file. I want to read the text file and store the data in my variables, then insert them in my function inserir_medico
that will use the data to create a concatenated List.
The problem is, I have a variable that counts the number of "doctors", this variable at the end of the program should be equal to 3 (conta_med = 3
), because I only have 3 doctors in my file (with their specialties and hours of entry and exit).
I wonder why in the end I get conta_med = 5
and not conta_med = 3
? Is it because you are working with a text and non-binary file?
The contents of the text file are:
Joao Silva
Neurology 9.30 - 17.00
Ana Maria Santos
Pediatrics 10.00 - 19.00
Sandra Almeida
Dermatology 14.00 - 17.45
Function that fetches the file data to place in the list widgets:
int med_ficheiro(){
FILE *fm; char mnome[50], esp[50]; int h_entrada, h_saida;
int conta_med=0;
int linhas[100], z=0, x=0;
char ch;
inic_med();
fm= fopen("medicos.txt","r");
if(fm==NULL){
printf("Impossivel abrir o ficheiro.\n");
exit(1);
}
else{
printf("\n\n\n");
while(!feof(fm)){
fgets(mnome,50,fm);
fscanf(fm,"%s %f - %f", esp, &h_entrada, &h_saida);
inserir_medico(mnome, esp, h_entrada, h_saida);
conta_med++;
}
printf("%d", conta_med); //AQUI DÁ ERRADO!!! PORQUE??? (MOSTRA "5")
}
}