From 17af47f525d0740377d1e1fc66a354193bc71f18 Mon Sep 17 00:00:00 2001 From: Mazen BEN GHORBAL Date: Tue, 10 Dec 2024 14:07:28 +0100 Subject: [PATCH] Ajouter exo3.c --- exo3.c | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 exo3.c diff --git a/exo3.c b/exo3.c new file mode 100644 index 0000000..e22a3c0 --- /dev/null +++ b/exo3.c @@ -0,0 +1,90 @@ +include +#include + +void deplacerPremierOctet(const char *filename) { + FILE *file; + unsigned char first_byte; + long file_size; + unsigned char *buffer; + + + file = fopen(filename, "r+b"); + if (file == NULL) { + perror("Erreur d'ouverture"); + exit(EXIT_FAILURE); + } + + if (fread(&first_byte, 1, 1, file) != 1) { + perror("Erreur de lecture"); + fclose(file); + exit(EXIT_FAILURE); + } + + fseek(file, 0, SEEK_END); + if (fwrite(&first_byte, 1, 1, file) != 1) { + perror("Erreur d'écriture"); + fclose(file); + exit(EXIT_FAILURE); + } + + file_size = ftell(file); + fclose(file); + + file = fopen(filename, "r+b"); + if (file == NULL) { + perror("Erreur d'ouverture"); + exit(EXIT_FAILURE); + } + + buffer = (unsigned char *)malloc(file_size - 1); + if (buffer == NULL) { + perror("Erreur d'allocation mémoire"); + fclose(file); + exit(EXIT_FAILURE); + } + + fseek(file, 1, SEEK_SET); + if (fread(buffer, 1, file_size - 1, file) != file_size - 1) { + perror("Erreur de lecture"); + free(buffer); + fclose(file); + exit(EXIT_FAILURE); + } + fclose(file); + + file = fopen(filename, "wb"); + if (file == NULL) { + perror("Erreur d'ouverture"); + free(buffer); + exit(EXIT_FAILURE); + } + + if (fwrite(buffer, 1, file_size - 1, file) != file_size - 1) { + perror("Erreur d'écriture"); + free(buffer); + fclose(file); + exit(EXIT_FAILURE); + } + + if (fwrite(&first_byte, 1, 1, file) != 1) { + perror("Erreur d'écriture"); + free(buffer); + fclose(file); + exit(EXIT_FAILURE); + } + + free(buffer); + fclose(file); + + printf("Modification effectuée.\n"); +} + +int main(int argc, char *argv[]) { + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + deplacerPremierOctet(argv[1]); + return 0; +} \ No newline at end of file