Reading file in C

4

I have the following structure for reading 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 an ENTER and goes to the second line, I just do not see where);
  • after printing the first line (which is actually the second line), the Third-party CPF is printed in isolation;
  • Third-party data is not printed, then jump to the customer, as, like the second customer, the data is printed correctly.
  • This sequence of problems is the pattern of the response.

    Each field in the file is separated by a comma, as you can see in the code above.

    Can anyone confirm me the infamous ENTER that is being read at some point that I can not detect? Or do you see some other error?

        
    asked by anonymous 08.11.2017 / 00:14

    1 answer

    2

    To begin with, since you're not playing in stdin , you do not have to fflush it.

    Another thing: using fgets will cause the file reading pointer to be moved forward. Using fscanf soon after will not be able to use this data because they have already been consumed!

    How to solve this? It has some ways ...

      

    As the input is not available, I can not try to run and check if really makes sense of the code; I did my best with the data provided.

    Return from fscanf

    The scanf function and its sisters return an integer. What does this whole mean? Simple, how many arguments were read. This function returns the amount of characters consumed by the read, or negative if you can not read anything. You can find the documentation here.

      

    I'm not sure about arguments of type %*

    This means that the function can be the parameter of while . In general, assuming your fscanf is correct:

    while (fscanf(arq, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]\n", cpf, nome, cnh, endereco, contato, passaporte, idoneo) > 0) { // tenta ler, se possível é um cliente válido
            printf("Cliente %d:\n", idx);
            idx++;
            // para cada linha capturada, atribui um valor a variável, para em seguida fazer a impressão
            // resto do seu código
    

    sscanf , reading a string

    Another alternative is to keep fgets and change the reading source. One of the functions of the scanf family is sscanf :

    • scanf read the default entry
    • fscanf read a given file
    • sscanf read a string

    The rest of the code is still identical , just change fscanf to this:

    sscanf(linha, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]\n", cpf, nome, cnh, endereco, contato, passaporte, idoneo);
    
        
    08.11.2017 / 01:19