4
I use the following structure to read each line of a file:
fclose(arq); // fecha o arquivo para em seguida abri-lo em modo leitura apenas
arq = fopen(url, "r");
if(arq == NULL) {
printf("Erro, nao foi possivel abrir o arquivo\n");
} else {
system("cls");
printf("\n **************************************** NOME DA EMPRESA - LISTA DE CLIENTES ****************************************\n\n");
fflush(stdin);
while (fgets(linha, TAM_BUFFER, arq)) { // lê cada linha do arquivo por vez, cada linha está na variável buffer
printf("Cliente %d:\n", idx);
idx++;
fflush(stdin);
// para cada linha capturada, atribui um valor a variável, para em seguida fazer a impressão
fscanf(arq, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]\n", cpf, nome, cnh, endereco, contato, passaporte, idoneo);
Problems:
- the first line is not printed (I believe the program is reading a ENTER and goes to the second line, I just don’t see where);
- after printing the first line (which is actually the second), the Third Client’s CPF is printed separately;
- the data of the third customer are not printed, hence jumps to the fourth customer, as well as second customer, the data are printed correctly.
This sequence of problems is the pattern of the answer.
Each field in the file is separated by a comma, as noted in the above code.
Can anyone confirm to me the infamous ENTER that is being read at some point that I cannot detect? Or see some other error?
Is there any way to post the output as text? I can’t see it from my phone
– Jefferson Quesado
You must have noticed you’re not reading all the odd customers, right? This is because you are consuming the file at two different points
– Jefferson Quesado
You mean in fgets and fscanf?
– Ângelo
Yes. I’m writing an answer detailing this
– Jefferson Quesado