From: daniel on 3 Mar 2010 17:10 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?
From: jeff on 3 Mar 2010 23:08 On Mar 3, 3:10 pm, daniel <daniel.bal...(a)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 = 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 ()
From: daniel on 4 Mar 2010 01:37 On Mar 4, 6:08 am, j...(a)automationservicesco.com wrote: > On Mar 3, 3:10 pm, daniel <daniel.bal...(a)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 = 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.
From: Jonathan de Boyne Pollard on 4 Mar 2010 07:17 <!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(a)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>
From: Michael Wojcik on 4 Mar 2010 14:23 daniel wrote: > On Mar 4, 6:08 am, j...(a)automationservicesco.com wrote: >> On Mar 3, 3:10 pm, daniel <daniel.bal...(a)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
|
Next
|
Last
Pages: 1 2 Prev: How to use tab control in vc++ win32 api (not mfc) ?? Next: Handling NM_RETURN in a List View |