Why fgetc(stdin) does not recognize the end of a file?

Asked

Viewed 33 times

0

For some reason the fgetc(stdin) is not recognizing the end of a file line and it is in the while infinitely.

fscanf(arqIN, "%*s %d", &qntPacientes); // Aqui é só pra pegar a informação da primeira linha
    
    pacientes = (Paciente**)malloc(qntPacientes * sizeof(Paciente*));
    
    if (pacientes == NULL){exit(1);}
    
    while(endF != 1) {
        index = 0;
        fscanf(arqIN, "%d", &atualPaciente);
        printf("Paciente: %d ", atualPaciente);
        
        while (ch = fgetc(arqIN) != '\n') {
            
            fscanf(arqIN, "%d", &resultados[index]);
            printf("%d ", resultados[index]);
            index++;
            tamanhoVetorResultados++;
        }
        printf("\n");
        
        pacientes[atualPaciente] = retornaResultados(resultados, tamanhoVetorResultados);
        
        if(ch == EOF) {endF = 1;}
    }

The input file is like this:

Clientes: 4
2 110 120 100 300 110
3 250
1 170 100
0 95
  • Would here: while (ch = fgetc(arqIN) != '\n') { you didn’t want to do while ((ch = fgetc(arqIN)) != '\n') {? Check the operators' priority.

  • I made the switch and yet it continues endlessly in the loop

  • In this test https://ideone.com/8RmeUy worked as expected.

  • I stopped to test here, and in Vscode gave a different result from onlineGDB(In case it worked in Vscode).

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.