Ver Mensaje Individual
  #13 (permalink)  
Antiguo 19/06/2010, 11:21
minette1988
 
Fecha de Ingreso: febrero-2010
Mensajes: 258
Antigüedad: 14 años, 3 meses
Puntos: 0
Respuesta: mostrar datos de un fichero binario por pantalla

Hola, quiero hacer el mismo ejercicio, pero usando la función calloc, puesto que ya sé de antemano cuántos registros voy a introducir, pero me da error: En la función ‘main’:
leer_fich_bin.c:50: error: expected expression before ‘)’ token

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