Advertisement
Trollkemada

Untitled

Mar 25th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. void guardarDatosAlumno(char * nombref,struct tAlumno p)
  2. {
  3.      FILE *archivo = fopen(nombref,"r");
  4.      if(archivo==NULL)
  5.      {
  6.         archivo=fopen(nombref,"wb");
  7.         if (archivo == NULL) {
  8.                     perror("Lol: ");
  9.         }else {
  10.               printf("No hubo errores");
  11.         }
  12.         // aqui deberías comprobar que se abre bien o mostrar error...
  13.         fwrite(&p,sizeof(struct tAlumno),1,archivo);
  14.         fclose(archivo);
  15.      }
  16.      else
  17.      {
  18.          FILE *archivo2;
  19.          archivo2=fopen("auxiliar","w");  // aux es un nombre reservado de sistema, mejor usa auxiliar para evitar problemas (aunque con aux quizá funcione bien)
  20.          struct tAlumno al;
  21.          fread(&al,sizeof(struct tAlumno),1,archivo);
  22.          while(!feof(archivo))
  23.          {
  24.              if(strcmp(p.dni,al.dni)<0)
  25.              {
  26.                             fwrite(&al,sizeof(struct tAlumno),1,archivo2);
  27.                             fread(&al,sizeof(struct tAlumno),1,archivo);
  28.              }  
  29.          }            
  30.          fwrite(&p,sizeof(struct tAlumno),1,archivo2);
  31.          while(!feof(archivo))
  32.          {
  33.                               fwrite(&al,sizeof(struct tAlumno),1,archivo2);
  34.          }
  35.          fclose(archivo);
  36.          fclose(archivo2);
  37.          remove(nombref);
  38.          rename("aux",nombref);
  39.      }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement