How should I manage ::std::cout after changing file descriptor 1 to refer to a different file?
问题 I would like to do dup2(fd, 1); close(fd); and have ::std::cout write to the new fd 1. How do I can reset the state of ::std::cout so nothing goes funny? For example, is flushing beforehand sufficient? Or is there more to do than that? I'm also curious about the same thing with ::std::cin . Is there a standard mechanism for resetting these if you change out the file descriptors they're using underneath them? To be clear, my goal here is basically to redirect my own input and output someplace