Correction : La manipulation des fichiers
Correction Exercice 1
Objectif : Lire dans un fichier
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * get_file(FILE * fp)
{
char * content;
char buffer[10];
content = malloc(sizeof(char));
content[0] = '\0';
while(fgets(buffer, 10, fp) != NULL)
{
content = realloc(content, (strlen(buffer) * sizeof(char)) + strlen(content) * sizeof(char));
content = strcat(content, buffer);
}
return content;
}
int main()
{
FILE *fp = fopen("exercice1.txt", "r");
char * file_content;
if (fp == NULL)
{
printf("Le fichier exercice 1.txt n'a pas pu être ouvert\n");
return EXIT_FAILURE;
}
file_content = get_file(fp);
printf("%s\n", file_content);
free(file_content);
fclose(fp);
return 0;
}
Correction Exercice 2
Objectif : remplacer le contenu d'un fichier
Frêre Jacques, frêre Jacques
Dormez-vous, dormez-vous ?
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("exercice2.txt", "w+");
char *text = "Frêre Jacques, frêre Jacques\n
Dormez-vous, dormez-vous ?";
if (fp == NULL)
{
printf("Le fichier texte.txt n'a pas pu être ouvert\n");
return EXIT_FAILURE;
}
fprintf(fp, "%s\n", text);
fclose(fp);
return 0;
}
Correction Exercice 3
Objectif : stocker le contenu d'un fichier dans un double tableau
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * get_file(FILE * fp)
{
char * content;
char buffer[10];
content = malloc(sizeof(char));
content[0] = '\0';
while(fgets(buffer, 10, fp) != NULL)
{
content = realloc(content, (strlen(buffer) * sizeof(char)) + strlen(content) * sizeof(char));
content = strcat(content, buffer);
}
return content;
}
int count_lines(char *str)
{
int i = 0;
int lines = 0;
while(str[i] != '\0')
{
if(str[i] == '\n')
{
lines++;
}
i++;
}
return lines + 1;
}
void display_tab(char **tab)
{
int i = 0;
while(tab[i] != NULL)
{
printf("%s\n", tab[i]);
i++;
}
}
int main()
{
//1. Ouvrir le fichier
FILE *fp = fopen("exercice3.txt", "r");
char * file_content;
char **tab;
int lines;
char * line;
int i = 0;
if (fp == NULL)
{
printf("Le fichier exercice3.txt n'a pas pu être ouvert\n");
return EXIT_FAILURE;
}
//2. Récupérer le contenu du fichier dans un `char *`
file_content = get_file(fp);
//3. Compter le nombre de lignes du fichier
lines = count_lines(file_content);
//4. Allouer la mémoire pour le double tableau `char **` et ses lignes
tab = malloc((lines + 1) * sizeof(*tab)); // <= le double tableau
tab[lines] = NULL;
line = strtok(file_content, "\n");
while(line != NULL)
{
tab[i] = malloc((strlen(line) + 1) * sizeof(char)); // <= chacune des lignes
//5. Remplir le double tableau avec le contenu du fichier
tab[i] = strcpy(tab[i], line);
i++;
line = strtok(NULL, "\n");
}
//6. Afficher le double tableau
display_tab(tab);
//7. Libérer la mémoire allouée
i = 0;
while(i < lines)
{
free(tab[i]);
i++;
}
free(tab);
//8. Fermer le fichier
fclose(fp);
return 0;
}
Correction Exercice 4
Objectif : stocker le contenu d'un fichier dans une structure
struct.h
#ifndef __STRUCT_H__
#define __STRUCT_H__
struct s_user {
char *first_name;
char *last_name;
int age;
};
typedef struct s_user user;
#endif
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "struct.h"
char * get_file(FILE * fp)
{
char * content;
char buffer[10];
content = malloc(sizeof(char));
content[0] = '\0';
while(fgets(buffer, 10, fp) != NULL)
{
content = realloc(content, (strlen(buffer) * sizeof(char)) + strlen(content) * sizeof(char));
content = strcat(content, buffer);
}
return content;
}
int main()
{
FILE *fp = fopen("exercice4.txt", "r");
char * file_content;
user hugues;
if (fp == NULL)
{
printf("Le fichier exercice4.txt n'a pas pu être ouvert\n");
return EXIT_FAILURE;
}
file_content = get_file(fp);
//hugues.first_name = malloc(25 * sizeof(char));
//hugues.last_name = malloc(25 * sizeof(char));
//sscanf(file_content, "Prénom : %s\nNom : %s\nAge : %d", hugues.first_name, hugues.last_name, &hugues.age);
sscanf(file_content, "Prénom : %ms\nNom : %ms\nAge : %d", &hugues.first_name, &hugues.last_name, &hugues.age);
printf("Mon nom est %s %s et j'ai %d ans.\n", hugues.first_name, hugues.last_name, hugues.age);
free(file_content);
free(hugues.first_name);
free(hugues.last_name);
fclose(fp);
return 0;
}
Correction Exercice 5
Objectif : stocker un double tableau dans un fichier
#include <stdlib.h>
#include <stdio.h>
int main()
{
char **tab = malloc(3 * sizeof(*tab));
int i = 0;
tab[0] = "Goodnight\n";
tab[1] = "And thanks !\n";
tab[2] = "For all the fish!\n";
FILE *fp = fopen("exercice5.txt", "w+");
if (fp == NULL)
{
printf("Le fichier exercice5.txt n'a pas pu être ouvert\n");
return EXIT_FAILURE;
}
while(i < 3)
{
fprintf(fp, "%s", tab[i]);
i++;
}
free(tab);
fclose(fp);
return 0;
}
Correction Exercice 6
Objectif : stocker une structure dans un fichier
struct.h
#ifndef __STRUCT_H__
#define __STRUCT_H__
struct s_movie {
char *title;
char *director;
int year;
};
typedef struct s_movie movie;
#endif
main.c
#include <stdlib.h>
#include <stdio.h>
#include "struct.h"
int main()
{
movie titanic;
titanic.title = "Titanic";
titanic.director = "James Cameron";
titanic.year = 1997;
FILE *fp = fopen("exercice6.txt", "w+");
if (fp == NULL)
{
printf("Le fichier exercice6.txt n'a pas pu être ouvert\n");
return EXIT_FAILURE;
}
fprintf(fp, "Titre : %s\nRéalisateur : %s\nAnnée de sortie : %d\n", titanic.title, titanic.director, titanic.year);
fclose(fp);
return 0;
}
Correction Exercice 7 : Dessine moi un mouton
Objectif : Dessiner un mouton en ASCII art en C et le stocker dans un fichier
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp = fopen("mouton.txt", "w+");
char *text = " _ _ _ _ _ _ _ _\n _-(_)- _-(_)- _-(_)- _-(_)- _-(_)- _-(_)- _-(_)- _-(_)-\n `(___) `(___) `(___) `(___) `(___) `(___) `(___) `(___)\n // \\\\ // \\\\ // \\\\ // \\\\ // \\\\ // \\\\ // \\\\ // \\\\ \n";
if (fp == NULL)
{
printf("Le fichier texte.txt n'a pas pu être ouvert\n");
return EXIT_FAILURE;
}
fprintf(fp, "%s\n", text);
return 0;
}
22 October 2025