views:

55

answers:

4

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
YES! Mad touch-typing skills FTW!
Jim Lewis
+1  A: 

You can use freopen() to redirect it to a file.

tomlogic
no is not low level actually the dup answer works thanks
Necronet
+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
+3  A: 

I believe dup2(fd,1) does the trick, e.g. after opening fd using open().

mvds
If you have a variable `FILE* f`, you can get the fd with `fileno(f)`. Something like this: `dup2(fileno(f), 1)`.
bstpierre
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