Function to read file in C

Asked

Viewed 95 times

1

What function to use in C to read file, row by row, and in each row contains different data types?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

2

Usually what you need is some variation of fscanf(). Eventually you can use too vfscanf().

-1

To read the file you can use the fgets :

#define N 10000
    [...]
      FILE *arquivo;
      arquivo=fopen("lugar.txt","r");
            if(arquivo == NULL)
            printf("Erro, nao foi possivel abrir o arquivo\n");
    else{
      while (fgets(linha, sizeof linha, arq3)) {
       //e se quiser escrever na tela na tela
       printf("%s",linha);
      }
     fclose(arquivo);
      }
  • Just one more question, in the fgets you used, in the line case, is a struct you created ?

  • line is a char vector

Browser other questions tagged

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