Hi all,
I have the following code snippet, that tries to redirect a program's
standard output/error to a file.
HANDLE fHandle;
fHandle = CreateFile(...);
SetStdHandle(STD_OUTPUT_HANDLE, fHandle);
SetStdHandle(STD_ERROR_HANDLE, fHandle);
fprintf(stdout, "string #1\n");
fprintf(stderr, "string #2\n");
....but it doesn't work. Output text goes to console instead of file.
Any ideas?
|
|
1
|
|
|
|
Reply
|
daniel.baluta (13)
|
3/3/2010 10:10:17 PM |
|
On Mar 3, 3:10=A0pm, daniel <daniel.bal...@gmail.com> wrote:
> Hi all,
>
> I have the following code snippet, that tries to redirect a program's
> standard output/error to a file.
>
> HANDLE fHandle;
>
> fHandle =3D CreateFile(...);
>
> SetStdHandle(STD_OUTPUT_HANDLE, fHandle);
> SetStdHandle(STD_ERROR_HANDLE, fHandle);
>
> fprintf(stdout, "string #1\n");
> fprintf(stderr, "string #2\n");
>
> ...but it doesn't work. Output text goes to console instead of file.
>
> Any ideas?
Try freopen () and/or fdopen ()
|
|
0
|
|
|
|
Reply
|
jeff
|
3/4/2010 4:08:24 AM
|
|
On Mar 4, 6:08=A0am, j...@automationservicesco.com wrote:
> On Mar 3, 3:10=A0pm, daniel <daniel.bal...@gmail.com> wrote:
>
>
>
> > Hi all,
>
> > I have the following code snippet, that tries to redirect a program's
> > standard output/error to a file.
>
> > HANDLE fHandle;
>
> > fHandle =3D CreateFile(...);
>
> > SetStdHandle(STD_OUTPUT_HANDLE, fHandle);
> > SetStdHandle(STD_ERROR_HANDLE, fHandle);
>
> > fprintf(stdout, "string #1\n");
> > fprintf(stderr, "string #2\n");
>
> > ...but it doesn't work. Output text goes to console instead of file.
>
> > Any ideas?
>
> Try freopen () and/or fdopen ()
Your idea seems to be fine but I want to understand why it doesn't
work in this situation.
thanks,
Daniel.
|
|
0
|
|
|
|
Reply
|
daniel
|
3/4/2010 6:37:21 AM
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<blockquote
cite="mid:2a312182-06e9-4c54-a80a-902b7d70b16f@f35g2000yqd.googlegroups.com"
type="cite">
<p wrap="">I have the following code snippet, that tries to redirect
a program's standard output/error to a file.<br>
</p>
<blockquote>
<pre>HANDLE fHandle = CreateFile(...);
SetStdHandle(STD_OUTPUT_HANDLE, fHandle);
SetStdHandle(STD_ERROR_HANDLE, fHandle);
fprintf(stdout, "string #1\n");
fprintf(stderr, "string #2\n");</pre>
</blockquote>
<p wrap="">...but it doesn't work. Output text goes to console
instead of file. Any ideas?<br>
</p>
</blockquote>
<p>By the time that your code comes to execute, your runtime library
has <em>already</em> queried Win32 for the standard handle numbers,
and saved them in internal library data structures. Whatever
subsequent alterations you make at the Win32 level, the <code>stdout</code>
and <code>stderr</code> streams end up using the Win32 handle numbers
that were already saved. You need to alter the runtime library's idea
of what those Win32 handles are. Of course, the mechanism for doing
this is highly specific to which C/C++ implementation you are using. <br>
</p>
<p>For OpenWatcom, for example, this involves the creative use of the <code>close()</code>
and <code>_open_osfhandle()</code> functions to re-map (library) file
descriptors 0, 1, and 2 to different underlying Win32 handles (obtained
from <code>CreateFile()</code> of course). This will make the
file-descriptor-level I/O functions such as <code>read()</code> and <code>write()</code>
use the new Win32 handles. One then needs to ensure that <code>stdin</code>,
<code>stdout</code>, and <code>stderr</code> are open and associated
with file descriptors 0, 1, and 2, if this is not already the case.<br>
</p>
</body>
</html>
|
|
0
|
|
|
|
Reply
|
Jonathan
|
3/4/2010 12:17:39 PM
|
|
daniel wrote:
> On Mar 4, 6:08 am, j...@automationservicesco.com wrote:
>> On Mar 3, 3:10 pm, daniel <daniel.bal...@gmail.com> wrote:
>>> I have the following code snippet, that tries to redirect a program's
>>> standard output/error to a file.
>>> HANDLE fHandle;
>>> fHandle = CreateFile(...);
>>> SetStdHandle(STD_OUTPUT_HANDLE, fHandle);
>>> SetStdHandle(STD_ERROR_HANDLE, fHandle);
>>> fprintf(stdout, "string #1\n");
>>> fprintf(stderr, "string #2\n");
>>> ...but it doesn't work. Output text goes to console instead of file.
>> Try freopen () and/or fdopen ()
>
> Your idea seems to be fine but I want to understand why it doesn't
> work in this situation.
stdout and stderr are not file handles. They're FILE* objects.[1]
When main is invoked, stdin, stdout, and stderr will have been created
for you by the environment[2] (modulo freestanding versus hosted
implementation, etc). They'll be created from the standard input,
output, and error handles, respectively.
But changing those handles after stdin, stdout, and stderr have been
created will make no difference to stdin et al. They've already been
created.
Changing STD_OUTPUT_HANDLE will affect subsequent writes *to that
handle*, and it'll be inherited by subsequent child processes (if you
don't override the child's standard handles in CreateProcess).
To change the FILE* objects that stdin, stdout, and stderr refer to,
you need to use freopen. That's the only mechanism endorsed by the C
standard.
Now, since some functions will write to the standard I/O FILE objects,
and some to the standard I/O handles, it's best to reassign both,
unless you know all the output you want redirected will be using one
abstraction or the other.
[1] "objects" in the C sense, not the C++ sense. (Of course in a
conforming C++ implementation, FILE will be a struct type, so stdout
and stderr will be pointers to C++ objects of FILE type.)
[2] "environment" as used in the C standard, not in the sense of a
collection of name=value string pairs accessible through getenv, etc.
--
Michael Wojcik
Micro Focus
Rhetoric & Writing, Michigan State University
|
|
0
|
|
|
|
Reply
|
Michael
|
3/4/2010 7:23:59 PM
|
|
This is a multi-part message in MIME format.
------=_NextPart_000_000E_01CABBD4.5DE6FB40
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Another possibility is to use the /ENTRY linker option to change the =
entry point to a routine that sets these values and then calls =
WinMainCRTStartup. This is also CRT and linker specific, but at least =
this way you sit under the CRT and there is no chance that the first few =
IOPs will be lost.
"Jonathan de Boyne Pollard" <J.deBoynePollard-newsgroups@NTLWorld.COM> =
wrote in message =
news:IU.D20100304.T121751.P2212.Q0@J.de.Boyne.Pollard.localhost...
I have the following code snippet, that tries to redirect a =
program's standard output/error to a file.
HANDLE fHandle =3D CreateFile(...);
SetStdHandle(STD_OUTPUT_HANDLE, fHandle);
SetStdHandle(STD_ERROR_HANDLE, fHandle);
fprintf(stdout, "string #1\n");
fprintf(stderr, "string #2\n");...but it doesn't work. Output text goes =
to console instead of file. Any ideas?
By the time that your code comes to execute, your runtime library has =
already queried Win32 for the standard handle numbers, and saved them in =
internal library data structures. Whatever subsequent alterations you =
make at the Win32 level, the stdout and stderr streams end up using the =
Win32 handle numbers that were already saved. You need to alter the =
runtime library's idea of what those Win32 handles are. Of course, the =
mechanism for doing this is highly specific to which C/C++ =
implementation you are using. =20
For OpenWatcom, for example, this involves the creative use of the =
close() and _open_osfhandle() functions to re-map (library) file =
descriptors 0, 1, and 2 to different underlying Win32 handles (obtained =
from CreateFile() of course). This will make the file-descriptor-level =
I/O functions such as read() and write() use the new Win32 handles. One =
then needs to ensure that stdin, stdout, and stderr are open and =
associated with file descriptors 0, 1, and 2, if this is not already the =
case.
------=_NextPart_000_000E_01CABBD4.5DE6FB40
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3Dtext/html;charset=3Diso-8859-1 =
http-equiv=3DContent-Type>
<META name=3DGENERATOR content=3D"MSHTML 8.00.7600.16490"></HEAD>
<BODY style=3D"PADDING-LEFT: 10px; PADDING-RIGHT: 10px; PADDING-TOP: =
15px"=20
id=3DMailContainerBody leftMargin=3D0 topMargin=3D0 bgColor=3D#ffffff =
text=3D#000000=20
CanvasTabStop=3D"true" name=3D"Compose message area">
<DIV><FONT size=3D2 face=3DArial>Another possibility is to use the =
/ENTRY linker=20
option to change the entry point to a routine that sets these values and =
then=20
calls WinMainCRTStartup. This is also CRT and linker specific, but =
at=20
least this way you sit under the CRT and there is no chance that the =
first few=20
IOPs will be lost.</FONT></DIV>
<BLOCKQUOTE=20
style=3D"BORDER-LEFT: #000000 2px solid; PADDING-LEFT: 5px; =
PADDING-RIGHT: 0px; MARGIN-LEFT: 5px; MARGIN-RIGHT: 0px"=20
dir=3Dltr>
<DIV>"Jonathan de Boyne Pollard" <<A=20
title=3D"mailto:J.deBoynePollard-newsgroups@NTLWorld.COM CTRL + =
Click to follow link"=20
=
href=3D"mailto:J.deBoynePollard-newsgroups@NTLWorld.COM">J.deBoynePollard=
-newsgroups@NTLWorld.COM</A>>=20
wrote in message <A=20
=
title=3D"news:IU.D20100304.T121751.P2212.Q0@J.de.Boyne.Pollard.localhost&=
#10;CTRL + Click to follow link"=20
=
href=3D"news:IU.D20100304.T121751.P2212.Q0@J.de.Boyne.Pollard.localhost">=
news:IU.D20100304.T121751.P2212.Q0@J.de.Boyne.Pollard.localhost</A>...</D=
IV>
<BLOCKQUOTE=20
=
cite=3Dmid:2a312182-06e9-4c54-a80a-902b7d70b16f@f35g2000yqd.googlegroups.=
com=20
type=3D"cite">
<P wrap=3D"">I have the following code snippet, that tries to =
redirect a=20
program's standard output/error to a file.<BR></P>
<BLOCKQUOTE><PRE>HANDLE fHandle =3D CreateFile(...);
SetStdHandle(STD_OUTPUT_HANDLE, fHandle);
SetStdHandle(STD_ERROR_HANDLE, fHandle);
fprintf(stdout, "string #1\n");
fprintf(stderr, "string #2\n");</PRE></BLOCKQUOTE>
<P wrap=3D"">...but it doesn't work. Output text goes to console =
instead of=20
file. Any ideas?<BR></P></BLOCKQUOTE>
<P>By the time that your code comes to execute, your runtime library =
has=20
<EM>already</EM> queried Win32 for the standard handle numbers, and =
saved them=20
in internal library data structures. Whatever subsequent =
alterations you=20
make at the Win32 level, the <CODE>stdout</CODE> and =
<CODE>stderr</CODE>=20
streams end up using the Win32 handle numbers that were already =
saved. =20
You need to alter the runtime library's idea of what those Win32 =
handles=20
are. Of course, the mechanism for doing this is highly specific =
to which=20
C/C++ implementation you are using. <BR></P>
<P>For OpenWatcom, for example, this involves the creative use of the=20
<CODE>close()</CODE> and <CODE>_open_osfhandle()</CODE> functions to =
re-map=20
(library) file descriptors 0, 1, and 2 to different underlying Win32 =
handles=20
(obtained from <CODE>CreateFile()</CODE> of course). This will =
make the=20
file-descriptor-level I/O functions such as <CODE>read()</CODE> and=20
<CODE>write()</CODE> use the new Win32 handles. One then needs =
to ensure=20
that <CODE>stdin</CODE>, <CODE>stdout</CODE>, and <CODE>stderr</CODE> =
are open=20
and associated with file descriptors 0, 1, and 2, if this is not =
already the=20
case.<BR></P></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_000E_01CABBD4.5DE6FB40--
|
|
0
|
|
|
|
Reply
|
m
|
3/5/2010 12:53:31 AM
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
<blockquote cite="mid:hmp5a401ljo@news6.newsguy.com" type="cite">
<p>Now, since some functions will write to the standard I/O <code>FILE</code>
objects, and some to the standard I/O handles, it's best to reassign
both, unless you know all the output you want redirected will be using
one abstraction or the other.</p>
</blockquote>
<p>Actually, there are three APIs, not two. I'm surprised to find that
although answers to this are frequently given, they're frequently
wrong, too, including one such answer that was published in <i>Windows
Developer Journal</i>. Hence I've written a Frequently Given Answer on
<a
href="http://homepage.ntlworld.com./jonathan.deboynepollard/FGA/redirecting-standard-io.html">redirecting
standard I/O from within a program</a>.</p>
</body>
</html>
|
|
0
|
|
|
|
Reply
|
Jonathan
|
3/7/2010 1:07:46 PM
|
|
Jonathan de Boyne Pollard wrote:
>
>> [I wrote:]
>> Now, since some functions will write to the standard I/O |FILE|
>> objects, and some to the standard I/O handles, it's best to reassign
>> both, unless you know all the output you want redirected will be using
>> one abstraction or the other.
>>
> Actually, there are three APIs, not two.
Quite right, and I ought to have mentioned that. Thanks for the
correction. (The third, for anyone who hasn't read the document
Jonathan posted the link to, is the POSIX API.)
I tend to avoid mixing the POSIX and Win32 APIs in the same program,
where possible, so I didn't think of mentioning the POSIX I/O
functions in my previous post. But even if the programmer writing the
main program avoids the POSIX functions, it's entirely possible
library code will call them.
--
Michael Wojcik
Micro Focus
Rhetoric & Writing, Michigan State University
|
|
0
|
|
|
|
Reply
|
Michael
|
3/8/2010 3:56:01 PM
|
|
|
7 Replies
1159 Views
(page loaded in 4.853 seconds)
Similiar Articles: Redirect stderr/stdout to a file using SetStdHandle - comp.os.ms ...Hi all, I have the following code snippet, that tries to redirect a program's standard output/error to a file. HANDLE fHandle; fHandle = Cre... nohup, redirection and splitting stderr and stdout - comp.unix ...Redirect stderr/stdout to a file using SetStdHandle - comp.os.ms ... nohup, redirection and splitting stderr and stdout - comp.unix ... Redirect stderr/stdout to a file ... How to test if stdout == stderr? - comp.unix.programmerRedirect stderr/stdout to a file using SetStdHandle - comp.os.ms ... How to test if stdout == stderr? - comp.unix.programmer Redirect stderr/stdout to a file using ... question about execl(), fork(), stdin, stdout, stderr - comp.unix ...... file. The problem is that this external program prints stuff to the STDOUT/STDERR ... using the following call after I fork another process execl ... Redirect stdout, stderr ... Write to stderr? - comp.lang.rexxRedirect stderr/stdout to a file using SetStdHandle - comp.os.ms ... how to write .test file using tcltest - comp.lang.tcl How to test if stdout == stderr? - comp.unix ... cron continues to email when no stdout/stderr from script - comp ...Redirect stderr/stdout to a file using SetStdHandle - comp.os.ms ... Redirect AT command Output to a File - comp.sys.hp.hpux cron continues to email when no stdout/stderr ... Capture the stdout of another process. - comp.unix.programmer ...Redirect stderr/stdout to a file using SetStdHandle - comp.os.ms ... Capture the stdout of another process. - comp.unix.programmer ... Redirect stderr/stdout to a file ... how to suppress error messages being outputted to the screen ...Redirect stderr/stdout to a file using SetStdHandle - comp.os.ms ... how to suppress error messages being outputted to the screen ... Redirect stderr/stdout to a file ... Capturing output of os.system to a string - comp.lang.python ...I guess I need to redirect stdout, but I'm a total beginner, and I ... Redirect stderr/stdout to a file using SetStdHandle - comp.os.ms ..... fprintf(stdout, "string #1 ... Redirecting Output to GUI - comp.soft-sys.matlabRedirecting Output to GUI - comp.soft-sys.matlab How can I also dump all the console output to a text file? - comp ... Redirect stderr/stdout to a file using SetStdHandle ... Re: Redirect stderr/stdout to a file using SetStdHandleAnother possibility is to use the /ENTRY linker option to change the entry point to a routine that sets these values and then calls WinMainCRTStartup. Redirect stderr/stdout to a file using SetStdHandle - comp.os.ms ...Hi all, I have the following code snippet, that tries to redirect a program's standard output/error to a file. HANDLE fHandle; fHandle = Cre... 7/26/2012 4:38:56 PM
|