Update 'ASR3.1/TP02 Fichier/copy2.c'

This commit is contained in:
lefevres 2021-10-04 13:36:54 +02:00
parent af5eae765f
commit 41c2fd6325

View File

@ -1,33 +1,36 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[]) { #define N 1
FILE *fileIn, *fileOut;
int val;
char ch;
if (argc!=3) { int main(int argc, char const *argv[]) {
printf("Il me faut deux noms de fichiers\n");
return EXIT_FAILURE; int fIn, fOut;
} char buff [N];
if ((fileIn= fopen(argv[1], "r")) == NULL) {
printf("Je ne peux pas ouvrir %s \n", argv[1]); fIn = open (argv[1], O_RDONLY);
return EXIT_FAILURE; assert (fIn >= 0);
}
if ((fileOut= fopen(argv[2], "w")) == NULL) { fOut = open (argv[2 ], O_WRONLY | O_CREAT, 0600);
printf("Je ne peux pas ouvrir %s\n", argv[2]); assert (fOut >=0);
return EXIT_FAILURE;
while (1){
ssize_t nbRead = read (fIn, buff, N);
if (nbRead <=0) {
break;
} else {
write (fOut, buff, N);
}
} }
while ((val=fgetc(fileIn)) !=EOF) { close (fIn);
printf("%d ", val); close (fOut);
fputc(val, fileOut);
ch = val;
printf("%d\n", ch);
}
fclose(fileIn);
fclose(fileOut);
return EXIT_SUCCESS;
} }