reroute printf outputs to a file

Hello
How is it able to reroute printf outputs into a file?
Must be simple but I didn’t find out yet.

Tobi

Try fprintf(fp,“msg”);
I usually make the fp = stdout, then redefine it if I want to output to a
file. If you mean that you want to redirect stdout, I guess you could just
redefine stdout = fp in the local file.

Regards,
David Kuechenmeister

“Tobias Moeglich” <Tobias.Moeglich@web.de> wrote in message
news:3E89B452.10307@web.de

Hello
How is it able to reroute printf outputs into a file?
Must be simple but I didn’t find out yet.

Tobi

Tobias Moeglich <Tobias.Moeglich@web.de> wrote:

Hello
How is it able to reroute printf outputs into a file?
Must be simple but I didn’t find out yet.

At the command line or from code?

At the command line:

program > filename

Or, if you want to append:

program >> filename

From code:

close(0);
open( “filename”, O_WRONLY|O_CREAT, … );

Or, if you want to append:

open("filename, O_WRONLY|O_CREAT|O_APPEND, … );

-David

QNX Training Services
http://www.qnx.com/support/training/
Please followup in this newsgroup if you have further questions.

Gibbs" <dagibbs@qnx.com> wrote in message news:b6cdks$ii5$1@nntp.qnx.com

close(0);
open( “filename”, O_WRONLY|O_CREAT, … );

That sounds scary, where does it say (in some form of standard) the open
will reuse first available fd starting from 0?

Better is freopen I beleive.

Mario Charest postmaster@127.0.0.1 wrote:

Gibbs" <> dagibbs@qnx.com> > wrote in message news:b6cdks$ii5$> 1@nntp.qnx.com> …

close(0);
open( “filename”, O_WRONLY|O_CREAT, … );

That sounds scary, where does it say (in some form of standard) the open
will reuse first available fd starting from 0?

Better is freopen I beleive.

From posix, on open()

“The open() function shall return a file descriptor for the named file
that is the lowest file descriptor not currently open for that process.
The open file description is new, and therefore the file descriptor
shall not share it with any other process in the system. The FD_CLOEXEC
file descriptor flag associated with the new file descriptor shall be
cleared.”

However of course you might get stung by another thread doing an open
in between your statements! ;v)


cburgess@qnx.com

But the scarier thing here is that 0 is standard input, not standard output!

dB

Colin Burgess <cburgess@qnx.com> wrote in message
news:b6cmnq$onp$1@nntp.qnx.com

Mario Charest postmaster@127.0.0.1 wrote:
Gibbs" <> dagibbs@qnx.com> > wrote in message
news:b6cdks$ii5$> 1@nntp.qnx.com> …

close(0);
open( “filename”, O_WRONLY|O_CREAT, … );

That sounds scary, where does it say (in some form of standard) the open
will reuse first available fd starting from 0?

Better is freopen I beleive.


From posix, on open()

“The open() function shall return a file descriptor for the named file
that is the lowest file descriptor not currently open for that process.
The open file description is new, and therefore the file descriptor
shall not share it with any other process in the system. The FD_CLOEXEC
file descriptor flag associated with the new file descriptor shall be
cleared.”

However of course you might get stung by another thread doing an open
in between your statements! ;v)


cburgess@qnx.com

David Bacon <dbacon@qnx.com> wrote:

But the scarier thing here is that 0 is standard input, not standard output!

Good catch, Dave… :v)

Colin Burgess <> cburgess@qnx.com> > wrote in message
news:b6cmnq$onp$> 1@nntp.qnx.com> …
Mario Charest postmaster@127.0.0.1 wrote:
Gibbs" <> dagibbs@qnx.com> > wrote in message
news:b6cdks$ii5$> 1@nntp.qnx.com> …

close(0);
open( “filename”, O_WRONLY|O_CREAT, … );

That sounds scary, where does it say (in some form of standard) the open
will reuse first available fd starting from 0?

Better is freopen I beleive.


From posix, on open()

“The open() function shall return a file descriptor for the named file
that is the lowest file descriptor not currently open for that process.
The open file description is new, and therefore the file descriptor
shall not share it with any other process in the system. The FD_CLOEXEC
file descriptor flag associated with the new file descriptor shall be
cleared.”

However of course you might get stung by another thread doing an open
in between your statements! ;v)


cburgess@qnx.com


cburgess@qnx.com