简体   繁体   中英

C Programming - File - fwrite

I've got a question regarding programming and files.

while(current!=NULL)
{
  if(current->Id_Doctor!='\0')
  {
    current=current->next;
    id_doc=(current->Id_Doctor);
  }
  if(current->Id_Doctor=='\0')
  {
    id_doc=id_doc+1;
    printf("%d", id_doc);
    break;
  }
}
fwrite(&id_doc, sizeof(char), 1, Archivo);

I dont know why but it aint writing the value of id_doc on the binary file called 'Archivo'...what could be the problem? I added a printf of id_doc and the value was printed..I really dont know

Ok, heres the full code(more-less):

struct Medico
{
  int Id_Doctor;
  int Estado;
  char Nombre[60];
  char Clave_Acceso[20];
  char Especialidad[40]; 
  struct Medico *next;
};
void Dar_Alta_Med (int estado);
void MenuPrincipal(char enta);
int main(void)
{
  char enta;
  MenuPrincipal(enta);
}
void Dar_Alta_Med(int estado)
{
  struct Medico * head = NULL;
  struct Medico * prev, *current;
  char nombre_doc[60], especialida[40], password[20];
  int id_doc=0, estado_doc=1;
  FILE *Archivo;
 const char *md1="\n<md>\n";
  const char *id_doc1="<id_doctor> ";
 Archivo=fopen("md.dat", "ab+");
  fwrite(md1, 1, strlen(md1), Archivo);
  fwrite(id_doc1, 1, strlen(id_doc1), Archivo);
  current = (struct Medico *) malloc (sizeof(struct Medico));
  current->Id_Doctor=id_doc; 
  while(current!=NULL)
    {
      if(current->Id_Doctor!='\0')
    {
      current=current->next;
      id_doc=(current->Id_Doctor);
    }
      else
    {
      id_doc=id_doc+1;
      printf("%d", id_doc);
      break;
    }
    }
  fwrite(&id_doc, sizeof(id_doc), 1, Archivo);
  printf("Ingresa el nombre del Doctor a dar de alta: ");
  fclose(Archivo);
}

Im dying here, please help :/

Try adding fflush(Archivo); to force a write of all buffered data.

Also, this statement: if(current->Id_Doctor=='\\0') really ought to be an else since there is no other thing it can be but '\\0'

Three things:

  • Make sure your fopen is successful.

     Archivo=fopen("md.dat", "ab+"); if (Archivo == NULL) { perror("Failed to open file Archivo"); ... } 
  • Make sure you are checking the success of your fwrite's.

     if (fwrite(&id_doc, sizeof(id_doc), 1, Archivo) < 1) { perror("Failed to write to file Archivo"); ... } 
  • Make sure you have a fclose to close the file properly.

     if (fclose(Archivo) != 0) { perror("Failed to close file Archivo"); ... } 

Now that you've post a full sample of your code I guess I should ask if error checking is just left out for brevity? If not, you should think about adding it.

If you're expecting the value of id_doc to be in display format in the output file you'll have to convert the int to a string (using snprintf or similar) and write the string to the output file instead.

fwrite(&id_doc, sizeof(char), 1, Archivo); 

If you defined id_doc as anything other than a char it will write \\0 to the file.

Much cleaner would be:

fwrite(&id_doc, sizeof(id_doc), 1, Archivo); 

If your first current is an Id_Doctor you have an endless loop.

If there is no current after your last current that is not an Id_Doctor , you get an illegal pointer derefenciation.

For your Problem: try the flush() family.

You're passing a pointer to a FOUR-BYTE INT, but only writing ONE BYTE (the wrong byte)!

Solution: declare id_doc as "char", not "int".

You have previously written the strings "\\n<md>\\n" and "<id_doctor> " to the file Archivo , which seems to indicate that it is not a binary file at all, but rather an XML-style file.

In this case, what you almost certainly want is:

fprintf(Archivo, "%d", id_doc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM