The following code is writing both “A” and “B” to the file “out.txt”, with the first call to open returning 3 and second call returning 4.
What I expected is for “A” to be written to the file and “B” to be written to the screen. I also expected open to return 3 in each case.
What should I do to fix the code below:
int main(int argc, char** argv)
{
int file = open("out.txt", O_APPEND | O_WRONLY);
if(file != 3) return 1;
if(dup2(file,1) < 0) return 1;
std::cout << "A" << std::endl;
if(dup2(1,file) < 0) return 1;
std::cout << "B" << std::endl;
file = open("out.txt", O_APPEND | O_WRONLY);
if(file != 3) return 1;
return 0;
}
Commenting from this link;
that is, it closes stdout and makes file descriptor 1 a clone of file descriptor 3.
So it does nothing since file descriptor 1 has the same value as file descriptor 3.
will open a file with the next available file descriptor. Since file descriptor 3 is busy, it will (in this case) use 4.
What you want to do is something more along the lines of;