From 41c2fd63257f5956f89e12fedf9ea216c49a405a Mon Sep 17 00:00:00 2001 From: lefevres Date: Mon, 4 Oct 2021 13:36:54 +0200 Subject: [PATCH] Update 'ASR3.1/TP02 Fichier/copy2.c' --- ASR3.1/TP02 Fichier/copy2.c | 53 ++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/ASR3.1/TP02 Fichier/copy2.c b/ASR3.1/TP02 Fichier/copy2.c index d65a6dc..e277870 100644 --- a/ASR3.1/TP02 Fichier/copy2.c +++ b/ASR3.1/TP02 Fichier/copy2.c @@ -1,33 +1,36 @@ -#include #include +#include +#include +#include +#include -int main(int argc, char *argv[]) { +#define N 1 - FILE *fileIn, *fileOut; - int val; - char ch; - if (argc!=3) { - printf("Il me faut deux noms de fichiers\n"); - return EXIT_FAILURE; - } - if ((fileIn= fopen(argv[1], "r")) == NULL) { - printf("Je ne peux pas ouvrir %s \n", argv[1]); - return EXIT_FAILURE; - } - if ((fileOut= fopen(argv[2], "w")) == NULL) { - printf("Je ne peux pas ouvrir %s\n", argv[2]); - return EXIT_FAILURE; +int main(int argc, char const *argv[]) { + + int fIn, fOut; + char buff [N]; + + fIn = open (argv[1], O_RDONLY); + assert (fIn >= 0); + + fOut = open (argv[2 ], O_WRONLY | O_CREAT, 0600); + assert (fOut >=0); + + while (1){ + ssize_t nbRead = read (fIn, buff, N); + + if (nbRead <=0) { + break; + } else { + write (fOut, buff, N); + } + } - while ((val=fgetc(fileIn)) !=EOF) { - printf("%d ", val); - fputc(val, fileOut); - ch = val; - printf("%d\n", ch); - } + close (fIn); + close (fOut); - fclose(fileIn); - fclose(fileOut); - return EXIT_SUCCESS; + } \ No newline at end of file