Program does not read file data correctly

2

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;

Then, 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 for the two files of the other structs)

And then, when the program is about to be closed and all 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, but the program was able to recover the data correctly. However, in this specific code, the data is partially recovered.

What could be happening in this case?

    
asked by anonymous 01.12.2014 / 22:45

1 answer

4

I think your problem is here:

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

...

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

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

According to the table of this site , these are the modes: / p>

  
  • r : Opens a text file for reading. The file must exist before it can be opened.
  •   
  • w : Opens a text file for recording. 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 recording. The data will be added at the end of the file (append), if it already exists, or a new file will be created in case of the file that did not exist previously.
  •   
  • rb : Opens a binary file for reading. Same as r previous mode, only the file is binary.
  •   
  • wb : Creates a binary file for writing, as in w previous mode, only the file is binary.
  •   
  • ab : Adds binary data at the end of the file, as in a mode, except that the file is binary.
  •   
  • r+ : Opens a text file for reading and writing. The file must exist and be able to be modified.
  •   
  • w+ : Creates a text file for reading and writing. If the file exists, the previous contents will be destroyed. If it does not exist, it will be created.
  •   
  • a+ : Opens a text file for recording and reading. The 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 exist previously.
  •   
  • r+b : Opens a binary file for reading and writing. Same as r+ above, only the file is binary.
  •   
  • w+b : Creates a binary file for reading and writing. Same as w+ above, only the file is binary.
  •   
  • a+b : Adds data or creates a binary file for reading and writing. Same as a+ above, only the file is binary.
  •   
    
02.12.2014 / 00:27