I'm trying to use redirects in C to redirect input to one file and then set standard output back to print to the screen. Could someone tell me what's wrong with this code?
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char** argv) {
//create file "test" if it doesn't exist and open for writing setting permissions to 777
int file = open("test", O_CREAT | O_WRONLY, 0777);
//create another file handle for output
int current_out = dup(1);
printf("this will be printed to the screen\n");
if(dup2(file, 1) < 0) {
fprintf(stderr, "couldn't redirect output\n");
return 1;
}
printf("this will be printed to the file\n");
if(dup2(current_out, file) < 0) {
fprintf(stderr, "couldn't reset output\n");
return 1;
}
printf("and this will be printed to the screen again\n");
return 0;
}
One thing you'll have to make sure to do before that will work at all, is to call fflush(stdout);
before switching the stdout
file descriptor out from under it. What's probably happening is that the C standard library is buffering your output, unaware that you're shifting around the file descriptors underneath it. The data you write using printf()
isn't actually sent to the underlying file descriptor until its buffer becomes full (or your program returns from main
).
Insert the call like this:
fflush(stdout);
if(dup2(file, 1) < 0) {
before both calls to dup2()
.
Your second dup2
call is wrong, replace with:
if (dup2(current_out, 1) < 0) {
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With