structure de matrice en C
je veux a partir d'un fichier recuperer une matrice voici en fait l'enonce:
1. struct matrix read_matrix(FILE * p) : cette méthode prend un pointeur sur un fichier correc-
tement ouvert et prêt à lire et lit la matrice et l’écrit dans la mémoire (à allouer) et renvoie un
pointeur vers la structure ainsi remplie.
et voici ce que j'ai fait :
matrix.h
Code:
1 2 3 4 5 6 7 8 9 10 11
|
#ifndef H_MATRIX
#define H_MATRIX
typedef struct{
int ** matrix;
int nb_lignes;
int nb_colonnes;} matrix;
matrix * read_matrix(FILE *p);
#endif |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| #include "matrix.h"
int main()
{
matrix *m;
FILE *p;
//p=fopen("test1","r");
m = read_matrix(p);
return 0;
}
matrix_io.c
1 #include <stdio.h>
2 #include<stdlib.h>
3 #include "matrix.h"
4
5
6 matrix *read_matrix(FILE *p)
7 {
8 // FILE *p;
9 p=fopen("test1","r");
10 matrix *m;
11 int i,j;
12 m=(matrix*)malloc(sizeof(matrix));
13 m->matrix=(int **)malloc(m->nb_lignes*m->nb_colonnes*sizeof(int*));
14 fscanf(p,"%i %i",&m->nb_lignes,&m->nb_colonnes);
15 printf("(%i %i)\n",m->nb_lignes,m->nb_colonnes);
16
17 for(i=0;i<=m->nb_lignes;i++)
18 {
19 for(j=0;j<20;j++)
20 for(j=0;j<m->nb_colonnes;j++)
21 {
22 fscanf(p,"%i",&m->matrix[i][j]);
23 }
24 //printf("%i\n",m->matrix[i][j]);*/
25 }
26 return m;
27 }
28 |
mais le prob est que j'arrive a lire et recuperer juste la 1ere ligne
Aussi si j'utilise comme parametre FILE *p j'obtients des erreurs de declaration et je ne sais pas comment jongler