Ver Mensaje Individual
  #8 (permalink)  
Antiguo 18/06/2010, 05:57
minette1988
 
Fecha de Ingreso: febrero-2010
Mensajes: 258
Antigüedad: 14 años, 2 meses
Puntos: 0
Respuesta: mostrar datos de un fichero binario por pantalla

He hecho la siguiente modificación:

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. struct registro{
  5.    char nombre[50];
  6. };
  7.  
  8.  
  9. int   main ()
  10.    {
  11.       FILE *fich;
  12.       char op;
  13.       int i;
  14.       int num_registros=0;
  15.       struct registro datos;
  16.       struct registro *ptr_reg;  
  17.  
  18.      
  19.       /* Escritura del float en el fichero */
  20.       if ((fich = fopen ("cadenas.dat", "wb")) == NULL)
  21.          {
  22.          printf ("Error de creación del fichero\n");
  23.          
  24.          }
  25.        else{
  26.           do{
  27.            printf("Introduce una cadena: ");
  28.            scanf("%49s",datos.nombre);
  29.            fwrite (&datos, sizeof (datos), 1, fich);
  30.            printf("¿Otro? (s/n)");
  31.            scanf("%1s",&op);
  32.           }while((op == 's') || (op == 'S'));
  33.        }
  34.        fclose (fich);
  35.  
  36.        /* Lectura del float del fichero */
  37.              
  38.       if ((fich = fopen ("cadenas.dat", "rb")) == NULL)
  39.          {
  40.          printf ("Error de existencia del fichero\n");
  41.          
  42.          }
  43.       else{
  44.            fseek(fich, 0, SEEK_END); // Colocar el cursor al final del fichero
  45.            num_registros = ftell(fich)/sizeof (struct registro); // Tamaño en bytes
  46.            ptr_reg = (struct registro*)malloc(sizeof(struct registro)*num_registros);
  47.            rewind(fich);
  48.            fread(ptr_reg, sizeof(struct registro), num_registros, fich);
  49.       }
  50.       fclose (fich);
  51.      
  52.      
  53.       for(i = 0; i < num_registros; i++)
  54.            printf("%s\n", ptr_reg[i].nombre);
  55. }

Cuando lo compilo me da este error: En la función ‘main’:
float.c:46: aviso: declaración implícita incompatible de la función interna ‘malloc’