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 <stdio.h>
#define MAX_LIGNE 10 /* au maximum, 10 lignes */
#define MAX_COL 8 /* au maximum 8 colonnes */
#define NOM_A_LIRE "C:\\test.txt" /*matrice a lire*/
typedef float Matrice[MAX_LIGNE] [MAX_COL];
/*fonction lire qui lit la matrice (sous forme de fichier .txt)*/
void lire(FILE * aLire, Matrice mat, int nbLigne, int nbCol) {
int i, j ;
for (i = 0 ; i < nbLigne; i++) {
for (j = 0 ; j < nbCol; j++)
fscanf(aLire, "%f", &mat[i] [j]);
fscanf(aLire, "\n" );
}
void afficher(Matrice mat, char nom[], int nbLigne, int nbCol)
{
int i, j ;
printf("\nContenu de la matrice %s de %d ligne(s) et %d colonne(s) :\n",nom, nbLigne, nbCol);
for (i = 0 ; i < nbLigne; i++) {
for (j = 0 ; j < nbCol; j++)
printf("%6.2f ", mat[i] [j]);
printf("\n" );
}
printf("\n" );
}
int main(void)
{
Matrice A;
int m, n;
/* ouvrir le fichier Matrices.dta sur le disque réseau pour la lecture */
FILE * aLire = fopen(NOM_A_LIRE, "r" ) ;
if(aLire == NULL)
{
printf("Le fichier n'existe pas" );
}
else
{ fscanf(aLire,"%d%d\n", &m, &n);
lire (aLire, A, m, n);
afficher(A, "A", m, n);
}
fclose(aLire);
return 0;
} |
Partager