From d7e5815872cd043f905114bffff608c7792cd417 Mon Sep 17 00:00:00 2001 From: Abdellah El Morabit Date: Sat, 22 Feb 2025 22:43:59 +0100 Subject: working on the making a copy of a file, so reading the contents and making a new file and writing to it the contents of the previous file --- main.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/main.c b/main.c index 21c65a1..b6d8828 100644 --- a/main.c +++ b/main.c @@ -158,6 +158,25 @@ void change_ownership(const char *path) { void copy_files(const char *arg) { // split the argument into the source and destination // make a copy of the source file in the destination file + int c; + + FILE *pold_file = fopen(argument.source, "r"); + if (pold_file == NULL) { + perror("cant open the source file for reading (copy failed) "); + return; + } + + FILE *pnew_file = fopen(argument.destination, "w"); + if (pnew_file == NULL) { + perror("cant open the destination file for reading (copy failed)"); + return; + } + + while ((c = fgetc(pold_file)) != EOF) { + fputc(c, pnew_file); + } + fclose(pnew_file); + fclose(pold_file); } void move_files(const char *arg) { -- cgit v1.2.3-70-g09d2