Jens said:
Can you name a single standard C function that writes to stderr
(of course except those output functions that you tell to write
to stderr - and for those it's already under your control where
they write to)?
Functions getopt() and getopt_long(). They write error messages to stderr
whenever an option is not recognized or there is something wrong with the
command line syntax defined by the application.
If you use freopen() to redirect stderr to a file then stderr gets
closed. If you somehow can get it back despite having it closed and
then re-redirect back to that again is a question that you have to
ask in a Windows group.
It should be basically as simple as initializing device stderr when the
program starts, shouldn't it? There is code within stdlib that creates an
__iob[2] array of FILE*, each being stdout, stdin and stderr, respectively.
If my programm succeeds in doing the same, i.e. re-initializing stderr with
the default values, the trick is done. So I asked the question if it was
possible.
Of course my program is (currently) a Windows application but - while I'm
writing it - I'm building a set of functions that I'll be able to reuse
under whatever platform I want, provided it's running GNU code.
What exactly did you do? Perhaps it's a buffering issue, i.e.
while stderr is line buffered the writes to new file are block-
buffered and, if your program crashes, the buffer isn't written
to the file?
I'm trying to create a library of functions that basically runs with C++
code but with a base set of C functions. That base set of C functions
should be able to handle multiple command line argument syntaxes, like
argtable2 but much simpler (for my own usage).
I've read argtable2 uses get_opt() and getopt_long() so is 100% compatible
with GNU getopt() syntax. However if you want multiple command line
syntaxes and are using only getopt() and getopt_long() both functions
output error messages with the syntaxes that don't match - since there
should be only one command line parameter syntax that matches but you have
to check them all in a while loop (for instance) until you find one that
matches, i.e. during which neither getopt() and getopt_long() return any
error message.
Hence I'd like to temporarily disable stderr while I'm checking command line
parameter syntaxes since I might get error messages, which is the normal
behaviour of these functions.
There's no 'iostream' in C, I guess you must be using C++, not C,
and then you should ask in comp.lang.c++.
Yes, I know there is no such iostream in C but iostream uses std* file
descriptors. Hence I suppose I must keep both libraries in sync, i.e. if I
close (or reopen) stderr cerr must be kept in sync so that it "knows" the
stderr file descriptor has changed.
I posted here for it's a C question first. And given the length of this
post, you can easily understand it's a hassle to type it again... ;-)
You would need to have a file name for stderr to be able to that
since the only function for redirection in standard C is freopen()
and this can only redirect to a file given by path, not to a FILE
pointer. If Windows supplies you with something in the file system
for (an already closed) stderr I can't tell, that you again need to
ask in a Windows group. But this solution then is already system
specific for this reason you probably also don't need to care if
the functions you use are standard C functions...
I'd say "no" since the libraries I'm using implement the same code on all
supported platforms (it's GNU code). So it's completely independent from
Windows. The only difference is there is no /dev/null, as when we redirect
output in shell scripts. I think that device is "nul:" under windows (I
remember batch scripts where I used:
do_something_with_possible_error_messages > nul:
The only thing I need to know, I suppose, is how does standard C library
initialize stderr so that I can do the same in my program - provided it's
safe, of course. Hence my question(s)...
Vince C.