Ver Mensaje Individual
  #6 (permalink)  
Antiguo 28/05/2014, 01:16
Avatar de Totti7
Totti7
 
Fecha de Ingreso: mayo-2012
Ubicación: barcelona
Mensajes: 28
Antigüedad: 12 años
Puntos: 2
Respuesta: leer fichero y guardarlo en matriz (2)

Muchas gracias a los dos, pero mas o menos con la idea i con los apuntes q tengo he hecho esto (al final entendi lo d linea por linea xD)
Nota: esto lo he hecho con el fscanf tal como tengo en los apuntes, luego probare con tu forma. Pero de esta manera me da un error en la llamada de la funcion en el main, donde pone leermatricula(*matricula)
Porque ? Graciass:D


Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define NumAlumnAsign 700
  5.  
  6.      typedef struct {
  7.         int nia;
  8.         int grado;
  9.         int lista_asign[NumAsign];
  10.      }matricula;
  11.  
  12.  
  13. void leermatricula(matricula *matricula){
  14.        
  15.     FILE *p;
  16.     char *a,line[300];
  17.     int c;
  18.     char *b;
  19.     int i;
  20.  
  21.         if (p= fopen("matricula.txt", "r"))
  22.          {
  23.               if(p==NULL){
  24.                 printf("Error: no se pudo abrir el archivo.\n");
  25.                 exit(1);
  26.                 }
  27.             else {
  28.                 c = fgetc(p);    
  29.                 while(c!= EOF){
  30.                 putchar(c);
  31.                 c = getc(p);
  32.  
  33.                     if((a = fgets(line, sizeof(line), p))) {
  34.                     printf("%s\n", line);
  35.  
  36.                         for (i=1; i<4; i++)
  37.                         {
  38.                             if(i==1)
  39.                             {
  40.                                 if((b = strtok(line, ","))) {
  41.                                 fscanf(p,"%d",matricula[i].nia);
  42.                                 }
  43.                             }
  44.  
  45.                             else if(i==2)
  46.                             {
  47.                                 if((b = strtok(line, ","))) {
  48.                                 fscanf(p,"%d",matricula[i].grado);
  49.                                 }
  50.                             }
  51.  
  52.                             else if(i==3)
  53.                             {
  54.                                 if((b = strtok(line, ")"))) {
  55.                                 fscanf(p,"%d",matricula[i].lista_asign);
  56.                                 }
  57.                             }
  58.  
  59.                         }
  60.      
  61.                     }
  62.                     else {
  63.                         if(ferror(p)) {
  64.                            
  65.                         }
  66.                         else if(feof(p)) {
  67.                            
  68.                         }
  69.                     }
  70.                 }
  71.             }
  72.               fclose(p);
  73.                
  74.          }
  75. }
  76.  
  77. main() {
  78.  
  79.   leermatricula(*matricula);
  80.  
  81. }