Entrainements + fin TP structures
This commit is contained in:
122
DEV1.1/TP23/TP22-reponses.txt
Normal file
122
DEV1.1/TP23/TP22-reponses.txt
Normal file
@@ -0,0 +1,122 @@
|
||||
----- TP22 : Fichiers (encore) -----
|
||||
|
||||
1.
|
||||
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
FILE* fichier = NULL;
|
||||
char* lu;
|
||||
char* contenu;
|
||||
int compteur = 1;
|
||||
fichier = fopen(argv[1], "r");
|
||||
if (fichier == NULL) {
|
||||
printf("Erreur d'écriture");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
lu = fgets(contenu, 1000, fichier);
|
||||
while (lu != NULL) {
|
||||
printf("%d ", compteur);
|
||||
printf(contenu);
|
||||
putchar('\n');
|
||||
lu = fgets(contenu, 1000, fichier);
|
||||
compteur++;
|
||||
}
|
||||
fclose(fichier);
|
||||
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
2.
|
||||
|
||||
int main(void) {
|
||||
/* Déclaration des tableaux */
|
||||
int t1[2][5];
|
||||
int t2[3][5];
|
||||
int t3[5][5];
|
||||
int compteur;
|
||||
/* Remplissage des tableaux */
|
||||
int i;
|
||||
int j;
|
||||
/* t1 */
|
||||
for (i = 0; i != 2; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
t1[i][j] = j + 1;
|
||||
}
|
||||
}
|
||||
/* t2 */
|
||||
compteur = 1;
|
||||
for (i = 0; i != 3; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
t2[i][j] = compteur;
|
||||
compteur++;
|
||||
}
|
||||
}
|
||||
/* t3 */
|
||||
compteur = 0;
|
||||
for (i = 0; i != 5; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
if (j >= i) {
|
||||
t3[i][j] = 0;
|
||||
}
|
||||
else {
|
||||
t3[i][j] = 1 + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Affichage des tableaux */
|
||||
/* t1 */
|
||||
for (i = 0; i != 2; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
printf("%d ", t1[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
/* t2 */
|
||||
for (i = 0; i != 3; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
printf("%d ", t2[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
/* t3 */
|
||||
for (i = 0; i != 5; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
printf("%d ", t3[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
/* Inversions lignes-colonnes */
|
||||
/* t1 */
|
||||
for (j = 0; j != 5; j++) {
|
||||
for (i = 0; i != 2; i++) {
|
||||
printf("%d ", t1[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
/* t2 */
|
||||
for (j = 0; j != 5; j++) {
|
||||
for (i = 0; i != 3; i++) {
|
||||
printf("%d ", t2[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
/* t3 */
|
||||
for (j = 0; j != 5; j++) {
|
||||
for (i = 0; i != 5; i++) {
|
||||
printf("%d ", t3[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
0
DEV1.1/TP23/t1.csv
Normal file
0
DEV1.1/TP23/t1.csv
Normal file
|
0
DEV1.1/TP23/t2.csv
Normal file
0
DEV1.1/TP23/t2.csv
Normal file
|
0
DEV1.1/TP23/t3.csv
Normal file
0
DEV1.1/TP23/t3.csv
Normal file
|
55
DEV1.1/TP23/test.c
Normal file
55
DEV1.1/TP23/test.c
Normal file
@@ -0,0 +1,55 @@
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
/* Déclaration des tableaux */
|
||||
int t1[2][5];
|
||||
int t2[3][5];
|
||||
int t3[5][5];
|
||||
FILE* fichier = NULL;
|
||||
int compteur;
|
||||
/* Remplissage des tableaux */
|
||||
int i;
|
||||
int j;
|
||||
/* t1 */
|
||||
for (i = 0; i != 2; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
t1[i][j] = j + 1;
|
||||
}
|
||||
}
|
||||
/* t2 */
|
||||
compteur = 1;
|
||||
for (i = 0; i != 3; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
t2[i][j] = compteur;
|
||||
compteur++;
|
||||
}
|
||||
}
|
||||
/* t3 */
|
||||
compteur = 0;
|
||||
for (i = 0; i != 5; i++) {
|
||||
for (j = 0; j != 5; j++) {
|
||||
if (j >= i) {
|
||||
t3[i][j] = 0;
|
||||
}
|
||||
else {
|
||||
t3[i][j] = 1 + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fichier = fopen("t1.csv", "w");
|
||||
if (fichier == NULL) {
|
||||
printf("Erreur d'ouverture.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
for (i = 0; i != 2; i++) {
|
||||
for (j = 0; j != 2; j++) {
|
||||
fputs(/* TODO */)
|
||||
}
|
||||
}
|
||||
fclose(fichier);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
11
DEV1.1/TP23/texte.txt
Normal file
11
DEV1.1/TP23/texte.txt
Normal file
@@ -0,0 +1,11 @@
|
||||
Le premier argument est l'adresse de la zone mémoire qui va recevoir les données. Le deuxième est la capacité de cette zone. Le troisième est le flux de lecture. Cette fonction va copier jusqu'à capacite-1 caractères, à moins qu'il ne rencontre la fin du fichier ou un saut de ligne. Le saut de ligne sera également transféré. Un marqueur de fin de chaîne sera ensuite ajouté.
|
||||
|
||||
La valeur renvoyée est l'adresse de la zone, ou NULL s'il y a eu une erreur ou si la fin du fichier est intervenue avant la lecture du moindre caractère.
|
||||
|
||||
La fonction fscanf a pour prototype :
|
||||
|
||||
int fscanf(FILE *flux, const char *format, ...);
|
||||
|
||||
Elle fonctionne sur le même principe que scanf, mais en ajoutant un paramètre pour indiquer depuis quel flux lire.
|
||||
|
||||
La valeur de retour est le nombre de valeurs interprétées, ou EOF en cas d'erreur ou si la fin du fichier est atteinte avant la moindre lecture. La constante EOF est obligatoirement négative (elle vaut -1 dans sa version GNU).
|
Reference in New Issue
Block a user