mfc >> HOW TO redirect stderr to a file??? (freopen NOT supported)

by Fabio » Fri, 21 Nov 2003 18:09:28 GMT

Hi i'm doing a porting from an application console for win32
to pocketpc 2002, this applicancion have a lot of call to function
similar to those:

fprintf(stderr, "%s: Unknown error\n", msg);
................
................
fprintf(stderr, "dropping packet #%u\n", seqno);
...............

i want to preserve those functions and redirect stderr to a file but
pocketpc sdk and EVC30 doe not support the function "freopen"
anyone can soggest me a way to do that redirection?

Thanks




mfc >> HOW TO redirect stderr to a file??? (freopen NOT supported)

by Joseph M. Newcomer » Sat, 22 Nov 2003 07:47:00 GMT


fclose(stderr);
FILE * f = fopen("whatever.txt", "w");
stderr = f;

At least that's how I used to do it decades ago on Unix, and I have no reason to expect
that Windows will run any differently.
joe




Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm



mfc >> HOW TO redirect stderr to a file??? (freopen NOT supported)

by Doug Harrison [MVP] » Sun, 23 Nov 2003 03:27:00 GMT





That's obviously wrong for at least a couple of reasons:

1. There's no guarantee that stderr is an lvalue, and in fact it isn't in
VC.

2. You might be able to get away with *stderr = *f, but it's undefined, and
thus it's a gamble. In particular, you wouldn't want to fclose(stderr)
before overwriting *stderr, so if that's what you meant, well... However,
you would want to fflush(stderr) before overwriting *stderr and orphaning
the file handle, buffer (if any), etc. Note that it's really kind of a
library glitch that it's even possible to copy FILE objects, because the
standard doesn't define what makes up the FILE struct, and it doesn't define
copying them. To the user, a FILE* is in all respects an opaque pointer.
Quiz time: Why then is struct FILE completely defined by <stdio.h>? Why
doesn't <stdio.h> simply declare FILE like this:

struct FILE;

Every library API is defined in terms of FILE*, so why isn't this
sufficient?

Anyway, the proper way to do this on Unix and other OSes whose compilers
implement a Unix-like I/O library layer has always been to use dup/dup2, or
as MS calls them, _dup/_dup2, e.g.

// NB: From distant memory, error-checking omitted.

// Flush any pending output.
fflush(stderr);

// Open new file.
int fd = _open(...);

// dup a copy of stderr's file handle, because dup2 will close it.
int orig = _dup(_fileno(stderr));

// Associate fd with stderr.
_dup2(fd, _fileno(stderr));

// We no longer need the handle fd, so close it.
_close(fd);

// Now output through stderr goes to the file opened above.

// When done writing, flush the stream.
fflush(stderr);

// Restore the original stderr file handle.
_dup2(orig, _fileno(stderr));

// And finally close the copy we made.
_close(orig);

// Now stderr is once again associated with its original file handle.

--
Doug Harrison
Microsoft MVP - Visual C++


HOW TO redirect stderr to a file??? (freopen NOT supported)

by Joseph M. Newcomer » Sun, 23 Nov 2003 18:00:19 GMT

OK, this is something I haven't done in a large number of years. I stand corrected.
joe





Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm


Similar Threads

1. C++: Redirecting stdin & stdout to same file with freopen

Hello,

I have an application where it would be convenient if I could redirect both 
stdin & stdout to a common file at the same time.  Then I could use cin to 
read from that file and cout to write to it.  In fact, with VS2005 on WinXP I 
have done so successfully with freopen, using the "r" mode for stdin and the 
"w" mode for stdout.  What bothers me is that I'm not sure whether doing such 
a thing is really legal and portable, and that I might be just "getting away" 
with doing it.  I need it to work with other compilers (GNU) and other 
operating systems too, namely UNIX, LINUX, MAC OS, and other mainstream OSes. 
 I do have a reason for needing to do this rather than simply opening the 
desired file directly in the first place.

Thanks,
Ray

2. Redirect stdout/stderr on spawned process (was TerminateProcess does not immediatly exit the process)

3. redirecting stdout with freopen on Win2003 and VC.NET

We use the following code section to reassign stdout to a 
text file. Subsequently calls to cout cause output to the 
created text file (logfile.txt):

     #include <iostream>
     using namespace std;

     // snip // snip // snip

     FILE *stream;
     stream = ::freopen("logfile.txt", "w", stdout );

     cout << "Some message to cout" << endl;

This worked fine on Windows 2000 and Visual Studio 6, 
however, on Windows 2003 and Visual Studio.NET this 
appears to work no longer. If after the last cout call we 
add the following lines:

     fprintf (stdout, "Another message to stdout!!!\n");

     fflush(stdout);

 then the second message does turn up in logfile.txt (the 
first one still doesn't).

N:B 
The above code runs in a non-console background 
application, and is located in a DLL. The same code still 
works OK in a console based .exe.

Does anyone have any ideas how we can fix this?
THANKS

4. child process with redirected stdout and stderr - CSharp/C#

5. Problems with redirect and format stdout and stderr for CreateProcess

6. Problems with redirect and format stdout and stderr for CreateProcess

7. How to redirect stdout, stderr in a GUI application

I would like to redirect stdout and stderr in a GUI application.
My GUI is composed of a mainframe with a ClistCrl(COutPutWnd) control on 
the bottom.
Inside my code I would like to be able to write :


cout << "test";

and to display it in my COutPutWnd.
How can I do that ?

8. freopen and fopen not thread safe?