Ver Mensaje Individual
  #1 (permalink)  
Antiguo 28/11/2012, 18:56
Avatar de rodrigoemece
rodrigoemece
 
Fecha de Ingreso: septiembre-2011
Mensajes: 68
Antigüedad: 12 años, 7 meses
Puntos: 1
Información Matrices y punteros

En el siguiente código tengo un problema al escanear la matriz de un archivo, alguien puede orientarme? Muchas gracias

Código C:
Ver original
  1. struct matriz{
  2.   float *datos;
  3.   int fil;
  4.   int col;
  5.   };
  6.  
  7. void ler(struct matriz *mt)
  8. {
  9.   int i=0;
  10.   int j=0;
  11.   FILE *fp;
  12.   fp = fopen("matrix.txt","rt");
  13.   if(fp==NULL)
  14.    {
  15.      printf("Erro na apertura do arquivo.\n");
  16.      exit(1);
  17.    }
  18.    
  19.      fscanf(fp,"%d",&mt[1].fil);
  20.      fscanf(fp,"%d",&mt[1].col);
  21.      mt[1].datos=(float*)malloc(mt[1].fil*mt[1].col*sizeof(float));
  22.      while(i<mt[1].fil)
  23.      {
  24.          while(j<mt[1].col)
  25.          {
  26.              fscanf(fp,"%f",(mt[1].datos + i*mt[1].col+j));
  27.              j++;
  28.          }        
  29.          i++;    
  30.      }        
  31.      
  32.      i=0;
  33.      j=0;
  34.      fscanf(fp,"%d",&mt[2].fil);
  35.      fscanf(fp,"%d",&mt[2].col);
  36.      mt[2].datos=(float*)malloc(mt[2].fil*mt[2].col*sizeof(float));
  37.      while(i<mt[2].fil)
  38.      {
  39.          while(j<mt[2].col)
  40.          {
  41.              fscanf(fp,"%f",(mt[2].datos + i*mt[2].col+j));
  42.              j++;
  43.          }        
  44.          i++;    
  45.      }  
  46.  
  47. }
  48.  
  49.  
  50.  
  51. main()
  52. {  
  53.   struct matriz mt[3];
  54.   ler(mt);
  55.   return 0;
  56.    
  57. }