Is there a way to redirect stdout with low level API... haven't found a function or a way to actually do this....
+2
A:
The dup
system call should let you redirect stdout, as shown in this example.
freopen
also will work, if you're redirecting to a file, but dup
can also be used
with pipes and sockets.
Jim Lewis
2010-07-17 00:16:07
YES! Mad touch-typing skills FTW!
Jim Lewis
2010-07-17 00:18:14
+1
A:
You can use freopen()
to redirect stdout to a file. If you're using a posix-like system, you can use close()
, dup()
and open()
, though these work with file descriptors and not FILEs.
Pablo Alejandro Costesich
2010-07-17 00:16:36
+3
A:
I believe dup2(fd,1) does the trick, e.g. after opening fd using open().
mvds
2010-07-17 00:16:42
If you have a variable `FILE* f`, you can get the fd with `fileno(f)`. Something like this: `dup2(fileno(f), 1)`.
bstpierre
2010-07-17 00:30:45
Thanks, never known how to mix up `FILE*` and regular `fd`s. I presume mixing them up in I/O is a recipe for disaster? (i.e. `FILE*` functions not knowing about your lower-level access to the `fd`)
mvds
2010-07-17 08:17:59