Advertisement
lencinasalejo

ArchivoTextoABinario

May 10th, 2019
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef struct{
  4.  int dni;
  5.  char apellido [21];
  6.  char nombre [21];
  7.  float prom;
  8. }t_estudiantes;
  9. int main()
  10. {
  11.    FILE *fp, *ft;
  12.    int r;
  13.    t_estudiantes est[]={{41222222, "Lencinas", "Alejo", 8.00},
  14.                         {4231523, "Bentivenga", "Simon", 9.00},
  15.                         {42315723, "Trillini", "Lucas", 5.00},
  16.                         {741236598, "Sol", "Federico", 8.75},
  17.                         {21346975, "Juan", "Lautaro", 8.95},
  18.                         {32156498, "Ruiz Diaz", "Manuel", 7.00},
  19.                         {2136548, "Klehr", "Brian", 4.00}};
  20.     fp=fopen("Estudiantestexto.dat", "w");
  21.     for(int i=0; i<7;i++){
  22.             fprintf(fp, "%08d%-20s%-20s%05.2f\n", est[i].dni, est[i].apellido, est[i].nombre, est[i].prom);}
  23.     fclose(fp);
  24.     ft=fopen("EstudiantesBinario.dat", "wb");
  25.     fp=fopen("Estudiantestexto.dat", "r");
  26.     t_estudiantes ests;
  27.     r=fscanf(fp,"%8d%20[^\n]%20[^\n]%5f\n", &ests.dni, &ests.apellido, &ests.nombre, &ests.prom );
  28.     while (!feof(fp)&&r==4){
  29.         fwrite(&ests, sizeof(t_estudiantes), 1, ft);
  30.         r=fscanf(fp, "%8d%20[^\n]%20[^\n]%5f\n", &ests.dni, &ests.apellido, &ests.nombre, &ests.prom );
  31.     }
  32.     fclose(fp);
  33.     fclose(ft);
  34.     ft=fopen("EstudiantesBinario.dat", "rb");
  35.     fread(&ests,sizeof(t_estudiantes), 1, ft);
  36.     while(!feof(ft)){
  37.         printf("%d %s %s %.2f\n", ests.dni, ests.apellido, ests.nombre, ests.prom);
  38.         fread(&ests,sizeof(t_estudiantes), 1, ft);
  39.     }
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement