Is it possible to open the file for writing and at the same time open for reading without first closing in C?

Is not a good programming practice?

void leitura(FILE *arq)
{
  if((arq = fopen("dificuldade.txt","r")) == NULL)
    printf("Erro ao abrir o arquivo\n"); 
  else
  {
    //linhas de codigo
    fclose(arq); 
  }
}

void impressao(FILE *arq)
{
  int i;

  if((arq = fopen("dificuldade.txt","w")) == NULL)
    printf("Erro ao abrir o arquivo\n");
  else
  {
    for(i=0; i<10; i++)
       {
         //linhas de codigos
         leitura(arq); 
         //Estou no loop e quero fazer a leitura a cada impressão
         //Cada impressão no ciclo, quero ler e manipular aqueles dados
         //Como faço isso, pois tenho que fechar o arquivo... ?
       }
    fclose(arq);
  }
}

main()
{
  FILE *arq;
  impressao(arq);
  return 0;
}
Author: Misaee 21, 2019-04-12

1 answers

It is good practice to close whenever you open a text file. With the table that is included in the attachment you can see some features that you can implement to read and write.

insert the description of the image here

These functions below serve to read and write to the text file.

void fprintf (FILE *fluxo, char *formatação, ...);
int fwrite (void *dados, int tamanho_do_elemento, int num_elementos, FILE *fluxo);

More information about text files I recommend wikibooks

 0
Author: Fábio Morais, 2019-04-14 00:50:39