1/ meme chose avec write(1); 2/ #include #include #include #include #define szbuf 256 int main(int argc, char const *argv[]) { char buf[szbuf]; int f1,f2,n,m; if(argc<3) { fprintf(stderr, "Usage : %s ",argv[1]); exit(1); } f1 = open(argv[1],O_RDONLY); if(f1 == -1) { perror("Opening source file fails"); exit(2); } f2 = open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0600); if(f2 == -1) { perror("Opening destination file fails"); exit(3); } while(n = read(f1,buf,szbuf)) { m = write(f2,buf,n); if(m == -1) { perror("Writing in file fails"); exit(4); } } close(f1); close(f2); exit(0); } 3/ #include #include #include #include #include #define szbuf 256 int main(int argc, char const *argv[]) { char buf[szbuf]; int a, b, c, d; if(argc<2) { fprintf(stderr, "Usage: %s ./store_numb ",argv[1]); exit(1); } a = open(argv[1],O_WRONLY|O_TRUNC|O_CREAT,0600); if(a == -1) { perror("Opening destination file fails"); exit(2); } b = open(argv[2],O_WRONLY|O_TRUNC|O_CREAT,0600); if(b == -1) { perror("Opening destination file fails"); exit(3); } write(1,"Numb --> ",9); memset(buf,0,szbuf); while(b = read(0,buf,szbuf)) { d = (int)strtol(buf,NULL,0); c = write(a,&d,sizeof(int)); if(c == -1) { perror("Writing in file fails"); exit(3); } memset(buf,0,szbuf); write(1,"Numb --> ",9); } close(a); exit(0); } 4/ #include #include #include #include #include #define szbuf 256 int main(int argc, char const *argv[]) { char buf[szbuf]; int a, b, c, d; if(argc<2) { fprintf(stderr, "Usage: %s ./store_numb ",argv[1]); exit(1); } a = open(argv[1],O_WRONLY|O_TRUNC|O_CREAT,0600); if(a == -1) { perror("Opening destination file fails"); exit(2); } write(1,"Numb --> ",9); memset(buf,0,szbuf); while(b = read(0,buf,szbuf)) { d = (int)strtol(buf,NULL,0); c = write(a,&d,sizeof(int)); if(c == -1) { perror("Writing in file fails"); exit(3); } memset(buf,0,szbuf); write(1,"Numb --> ",9); } close(a); exit(0); }