© Dunod. La photocopie non autorisée est un délit. Corrigés d) void Affichearticlecode(TypeArticle * tableau, int code, int nb) { int i, a; for (i = 0; i < nb; i++) { if (tableau[i].code == code) { printf(\"code %d : denomination=%s, prix=%f, stock=%d \\n\", tableau[i].code, tableau[i].denomination, tableau[i].prix, tableau[i].stock); break; } } printf(\"Le code recherché n’existe pas\"); } e) int Modifiearticlecode(char *nomfichier, int code) { int i, nb; TypeArticle Article; FILE *fp; if ((fp = fopen(nomfichier, \"r+b\")) == NULL) { puts(\"permission non accordée ou répertoire inexistant\"); } fread(&nb, sizeof(int), 1, fp); printf(\"nb=%d\\n\", nb); for (i = 0; i < nb; i++) { fread(&Article.code, sizeof(int), 1, fp); printf(\"code=%d\\n\", code); if (Article.code == code) { printf(\"Entrer la nouvelle nomination\\n\"); scanf(\"%s\", Article.denomination); fscanf(stdin, \"%*c\"); Article.denomination[strlen(Article.denomination)] = ’\\0’; printf(\"Entrer le nouveau prix\\n\"); scanf(\"%f\", &Article.prix); printf(\"Entrer le nouveau stock\\n\"); scanf(\"%d\", &Article.stock); fwrite(Article.denomination, 100, 1, fp); fwrite(&Article.prix, sizeof(float), 1, fp); 139
Chapitre 14 • Fichiers binaires fwrite(&Article.stock, sizeof(int), 1, fp); fclose(fp); return (0); } fseek(fp, (100 + sizeof(float) + sizeof(int)), SEEK_CUR); } printf(\"article non trouvé\\n\"); fclose(fp); return 1; } f) void Saisienouveauarticle(char *nomfichier) { int i, a, nb; TypeArticle Article; FILE *fp; fp = fopen(nomfichier, \"r+b\"); if (fp == NULL) { fprintf(stderr, \"Problème fopen\"); exit(1); } fread(&nb, sizeof(int), 1, fp); printf(\"Saisienouveau article=%d\\n\", nb); printf(\"Entrer le code de l’article à rajouter\\n\"); scanf(\"%d\", &Article.code); printf(\"Entrer la nouvelle nomination\\n\"); scanf(\"%s\", Article.denomination); fscanf(stdin, \"%*c\"); Article.denomination[strlen(Article.denomination) - 1] = ’\\0’; printf(\"Entrer le nouveau prix\\n\"); scanf(\"%f\", &Article.prix); printf(\"Entrer le nouveau stock\\n\"); scanf(\"%d\", &Article.stock); nb = nb + 1; fseek(fp, 0, SEEK_SET); fwrite(&nb, sizeof(int), 1, fp); fseek(fp, 0, SEEK_END); fwrite(&Article.code, sizeof(int), 1, fp); fwrite(Article.denomination, 100, 1, fp); fwrite(&Article.prix, sizeof(float), 1, fp); fwrite(&Article.stock, sizeof(int), 1, fp); fclose(fp); } 140
© Dunod. La photocopie non autorisée est un délit. Corrigés g) int Suprimearticle(char *nomfichier, int code) { int i = 0, nb, flag = 0; TypeArticle Article; FILE *fp; if ((fp = fopen(nomfichier, \"r+b\")) == NULL) { puts(\"permission non accordée ou répertoire inexistant\"); } fread(&nb, sizeof(int), 1, fp); while (i < nb && flag == 0) { fread(&Article.code, sizeof(int), 1, fp); if (Article.code == code) { flag = 1; } i++; fseek(fp, 100 + sizeof(float) + sizeof(int), SEEK_CUR); } if (flag == 0) { printf(\"article non trouvé\\n\"); fclose(fp); return 1; } if (flag == 1 && i == nb) /* l’article à supprimer est le dernier */ { fseek(fp, 0, SEEK_SET); nb = nb - 1; fwrite(&nb, sizeof(int), 1, fp); fclose(fp); return 1; } while (i < nb) { fread(&Article.code, sizeof(int), 1, fp); fread(Article.denomination, 100, 1, fp); fread(&Article.prix, sizeof(float), 1, fp); fread(&Article.stock, sizeof(int), 1, fp); fseek(fp, -2 * (sizeof(int) + 100 + sizeof(float) + sizeof(int)), SEEK_CUR); fwrite(&Article.code, sizeof(int), 1, fp); 141
Chapitre 14 • Fichiers binaires fwrite(Article.denomination, 100, 1, fp); fwrite(&Article.prix, sizeof(float), 1, fp); fwrite(&Article.stock, sizeof(int), 1, fp); i++; fseek(fp, 1 * (sizeof(int) + 100 + sizeof(float) + sizeof(int)), SEEK_CUR); } fseek(fp, 0, SEEK_SET); nb = nb - 1; fwrite(&nb, sizeof(int), 1, fp); fclose(fp); return 0; } 142
15TABLEAUX À DOUBLE ENTRÉE 15.1 TABLEAUX DE DIMENSION 2 Un tableau de dimension 2, aussi appelé tableau à double entrée, ou matrice, est un tableau de tableaux. On peut déclarer un tableau de dimension 2 statiquement : #define NB_LIGNES 100 #define NB_COLONNES 50 ... int tableau[NB_LIGNES][NB_COLONNES]; On accède aux différents éléments par des crochets : l’élément tableau[i] est un tableau, et son élément j est donc noté tableau[i][j]. Conventionnellement, on conçoit un tel tableau comme ayant des lignes et des colonnes, l’indice i repré- sentenant le numéro d’une ligne, et l’indice j représentant le numéro d’une colonne. L’élément tableau[i][j] se trouve à l’intersection de la ligne i et de la colonne j. © Dunod. La photocopie non autorisée est un délit. Exemple Le programme suivant crée une matrice dont chaque élément (i, j) vaut i + j. #define NB_LIGNES_MAX 100 #define NB_COLONNES_MAX 50 void Creer(int tableau[NB_LIGNES_MAX][NB_COLONNES_MAX], int nbl, int nbc) { int i, j; for (i=0 ; i<nbl ; i++) /* pour chaque ligne du tableau */ for (j=0 ; j<nbc ; j++) /* pour chaque colonne */ tableau[i][j] = i+j; } void Afficher(int tableau[NB_LIGNES_MAX][NB_COLONNES_MAX], int nbl, int nbc) { int i, j; for (i=0 ; i<nbl ; i++) /* pour chaque ligne */ { for (j=0 ; j<nbc ; j++) /* pour chaque colonne */ printf(\"%d \", tableau[i][j]); printf(\"\\n\"); /* on va à la ligne */ } } 143
Chapitre 15 • Tableaux à double entrée int main(void) { int tableau[NB_LIGNES_MAX][NB_COLONNES_MAX]; int nbl, nbc; puts(\"Entrez le nombre de lignes et de colonnes\"); scanf(\"%d %d\", &nbl, &nbc); if (nbl > NB_LIGNES_MAX || nbc > NB_COLONNES_MAX) { puts(\"Erreur, nombre dépassant la taille du tableau\"); exit(1); } Creer(tableau, nbl, nbc); Afficher(tableau, nbl, nbc); return 0; } Le résultat de l’exécution de ce programme pour 4 lignes et 6 colonnes est : 012345 123456 234567 345678 15.2 ALLOCATION DYNAMIQUE ET LIBÉRATION D’UN TABLEAU DE DIMENSION 2 Un tableau (d’entiers) de dimension 2 avec allocation dynamique se déclare par un double pointeur : int** tableau; L’idée est qu’un tableau d’entiers est de type int* et un tableau de tableaux d’en- tiers est donc de type (int*)*. Pour allouer le tableau de nbl lignes et nbc colonnes, on commence par allouer un tableau de nbl pointeurs. On alloue ensuite chacun de ces pointeurs avec nbc entiers dans une boucle (voir la figure 15.2). /* La fonction retourne le tableau à double entrée */ int** Allocation(int nbl, int nbc) { int **tab; int i; /* 1) allocation d’un tableau de nbl pointeurs */ tab = (int**)calloc(nbl, sizeof(int*)); 144
Ø Ð Ù ÔÓ ÒØ ÙÖ× 15.2. Allocation dynamique et libération d’un tableau de dimension 2 ¡¡¡ Ø Ð ÙÜ ³ ÒØ Ö× ¡¡¡ ¡¡¡ © Dunod. La photocopie non autorisée est un délit. ¡¡¡ ¡¡¡ ¡¡¡ ¡¡¡ ¡¡¡ Ø ÐÙ /* 2) allocation d’un tableau de nbc entiers */ /* au bout de chaque pointeur */ for (i=0 ; i<nbl ; i++) tab[i] = (int*)calloc(nbc, sizeof(int)); return tab; } Pour libérer la mémoire d’un tableau de dimension 2 alloué dynamiquement, il faut libérer un à un tous les tableaux avec une boucle, puis libérer le tableau de pointeurs. void Libération(int **tab, int nbl, int nbc) { int i; for (i=0 ; i<nbl ; i++) if (nbc>0) free(tab[i]); if (nbl>0) free(tab); } Il faut toujours respecter le principe d’un appel à free pour chaque appel à malloc ou calloc pour libérer toute la mémoire. 145
Search
Read the Text Version
- 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
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334