Ver Mensaje Individual
  #2 (permalink)  
Antiguo 16/11/2014, 06:51
Avatar de leosansan
leosansan
 
Fecha de Ingreso: mayo-2012
Ubicación: GRAN CANARIA
Mensajes: 194
Antigüedad: 12 años
Puntos: 49
Respuesta: Rellenar matriz desde un fichero en C

Cita:
Iniciado por nephalot Ver Mensaje
Hola a todos, estoy haciendo un trabajo y necesito llenar una matriz con números ingresados desde un txt pero cuando lo corro me dice que dejo de funcionar, obviamente creo que está malo jeje, pero no sé como hacerlo, si alguien me puede ayudar le estaré muy agradecida :)
................................

Y en en la consola se imprimió:

8 2 9 4 6 1 7 5 3
5 3 1 8 7 2 9 6 4
7 6 4 5 9 3 1 2 8
4 7 3 6 2 9 5 8 1
1 8 6 7 4 5 2 3 9
2 9 5 1 3 8 4 7 6
9 1 7 3 5 6 8 4 2
6 5 2 9 8 4 3 1 7
3 4

Osea faltaron algunos números, no entiendo por qué u.u


Este es el trozo de código original, el tipo de dato es entero.

Código C:
Ver original
  1. for(i=0;i<9;i++){
  2.         for(j=0;j<9;j++){
  3.               fscanf(f,"%d",&temp);
  4.               matriz[i][j] = temp;
  5.          }
  6.  }

gracias!!
Pues a mi se me imprime correctamente. Te dejo mi código no sea que hagas algo "raro" en el resto del código:

Código C++:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void) {
  5.     FILE *fichero;
  6.     int i, j, temp, matriz [9][9];
  7.     if ((fichero = fopen("matriz.txt", "r")) == NULL) {
  8.         perror("matriz.txt");
  9.         return EXIT_FAILURE;
  10.     }
  11.      
  12.     for(i=0;i<9;i++) {
  13.         printf(" \n");
  14.             for(j=0;j<9;j++) {
  15.               fscanf(fichero,"%d",&temp);
  16.                 matriz[i][j] = temp, printf("%d ", matriz[i][j]);
  17.                            
  18.        }
  19.     }
  20.     fclose(fichero);
  21.     return 0;
  22. }

¡¡¡Saluditos!!!