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 44 45 46 47 48 49 50 51 52
| void lectimage(short sim[NBLIG][NBCOL], short nbl, short nbc)
{
short *p, i, j;
long n, taille;
char *q = NULL, *buf = NULL;
FILE *fich = NULL;
char nom[40] = { '\0' };
printf("Nom du fichier image : ");
fgets(nom,39,stdin);
* strchr(nom,'\n') = '\0';
fprintf(stdout,"Nom entre '%s'\n",nom);
fich = fopen(nom,"r");
if (fich == 0)
{
printf(" ERREUR: fichier non existant\n");
exit(1);
}
taille = nbl * nbc;
q = buf = malloc(taille * sizeof(char));
if (!q)
{
printf(" ERREUR: la fonction malloc() ne peut plus allouer de l'espace:\n");
fclose(fich);
exit(1);
}
n = fread(q, sizeof(char), taille, fich);
printf(" valeur retournée par le fread est %d\n", n);
if (n - taille)
{
printf(" ERREUR: le nombre d'elements lus est incompatible\n");
free(q);
fclose(fich);
exit(1);
}
//Que fait ce bout de code?
for (q = buf, i = 0; i < nbl; i++)
{
for (p = &sim[i][0], j = 0; j < nbc; j++, q++, p++)
{
*p = *q & 0x00ff;
}
}
free(buf);
fclose(fich);
} |
Partager