I have a problem with dup and dup2.
int copy (char * file1, char * file2) {
int dr = open(file1, O_RDONLY);
if (dr == -1) {
return -1;
}
int dw = open(file2, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (dw == -1) {
return -1;
}
int save0 = dup(0);
int save1 = dup(1);
dup2(dr, 0);
close(dr);
dup2(dw, 1);
close(dw);
char c;
while ((c = getchar()) != EOF) {
putchar(c);
}
dup2(save0, 0);
dup2(save1, 1);
printf("Hi");
return 0;
}
I don't understand why the data from my file file1 is displayed on the screen because dr is standard input and dw is standard output...
Thanks for the help.