Program does not read file data properly

Asked

Viewed 278 times

3

I have these three structs:

   typedef struct {
     char codigopaciente[10];
     char nome[50];
     char telefone[50];
     int idade;
     char sexo;
  } paciente;

  typedef struct {
     char codigomedico[10];
     char nome[50];
     char especialidade[50];
     float valorconsulta;
 } medico;

 typedef struct {
     char codigopaciente[10];
     char codigomedico[10];
     char dataconsulta[10];
     char diadasemana[10];
 } consulta;

Hence, I use this code in main:

arquivo1 = fopen("medico.dat", "r");
// Se o arquivo for vazio, a memória é alocada.
if (arquivo1 == NULL) {
    printf("Quantos medicos voce deseja cadastrar?\n");
    scanf("%d", &quantidade_medicos);
    m = malloc(quantidade_medicos * sizeof(medico));
    medicos_cadastrados = 0;
// Se o arquivo não for vazio, a memória é alocada junto com os registros presentes no arquivo.
} else {
    fread(&quantidade_arquivo, sizeof(int), 1, arquivo1);
    printf("Existem %d medicos nesse arquivo. Quantos mais voce precisa cadastrar?\n", quantidade_arquivo);
    scanf("%d", &quantidade_medicos);
    quantidade_medicos = quantidade_medicos + quantidade_arquivo;
    m = malloc(quantidade_medicos * sizeof(medico));
    quantidade_medicos = quantidade_arquivo;
    fread(m, sizeof(medico), quantidade_arquivo, arquivo1);
    fclose(arquivo1);
    }

(the code is similar to the two files of the other structs)

And then, when the program is to be closed and all the writing operations have been done, I use this code:

   arquivo1 = fopen("medico.dat", "w");
   fwrite(&medicos_cadastrados, sizeof(int), 1, arquivo1);
   fwrite(m, sizeof(medico), (medicos_cadastrados), arquivo1);
   free(m);
   fclose(arquivo1);
   arquivo2 = fopen("paciente.dat", "w");
   fwrite(&pacientes_cadastrados, sizeof(int), 1, arquivo2);
   fwrite(p, sizeof(paciente), (pacientes_cadastrados), arquivo2);
   free(p);
   fclose(arquivo2);
   arquivo3 = fopen("consulta.dat", "w");
   fwrite(&consultas_cadastradas, sizeof(int), 1, arquivo3);
   fwrite(c, sizeof(consulta), (consultas_cadastradas), arquivo3);
   free(c);
   fclose(arquivo3);

I already used the same technique in another exercise and the generated files were not "readable" in a text editor, however, the program could recover the data correctly. However, in this specific code, the data is partially recovered.

What might be going on in this case?

1 answer

4


I think your problem is here:

arquivo1 = fopen("medico.dat", "r");

...

arquivo3 = fopen("consulta.dat", "w");

I think you should wear rb and wb instead of r and w. Or else some other way like ab, ab+, w+ or w+b. The important thing is to keep the bof binary, because its files are in binary mode (read with fread and written with fwrite), and not text mode.

According to the table of this website, these are the ways:

  • r: Opens a text file for reading. The file must exist before opening.
  • w: Opens a text file for saving. If the file does not exist, it will be created. If it already exists, the previous content will be destroyed.
  • a: Opens a text file for saving. The data will be added at the end of the file ("append"), if it already exists, or a new file will be created in the case of the previously non-existent file.
  • rb: Open a binary file for reading. Same as mode r previous, only that the file is binary.
  • wb: Create a binary file for writing, as in mode w previous, only that the file is binary.
  • ab: Adds binary data at the end of the file, as in mode a previous, only that the file is binary.
  • r+: Opens a text file for reading and writing. The file must exist and can be modified.
  • w+: Creates a text file for reading and writing. If the file exists, the previous content will be destroyed. If it does not exist, it will be created.
  • a+: Opens a text file for writing and reading. Data will be added at the end of the file if it already exists, or a new file will be created in case the file does not previously exist.
  • r+b: Opens a binary file for reading and writing. Same as r+ above, only that the file is binary.
  • w+b: Creates a binary file for reading and writing. The same as w+ above, only that the file is binary.
  • a+b: Adds data or creates a binary file for reading and writing. The same as a+ above, only that the file is binary.
  • It worked perfectly. But I was curious because I had cases where it worked with r and with w normally and in others gave problem.

  • 1

    @athosbr99 Probably because in most cases the result is the same in any mode, either text or binary. But in some, the result is different. In particular, line-break characters (\r and \n) has different treatment between text mode and binary mode. When represented as numbers, they are 13 and 10, respectively.

Browser other questions tagged

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