Hello,
I have C++ library with /clr support which links a static library
written in "C". The static library performs some operations and
outputs to "stderr" if an error is occured. I want to log the messages
in stderr in the main library. However, when I try to read stderr as
given below:
char buffer[4096];
fscanf(stderr, "%s", buffer);
I get strange characters in the buffer, not the message. However, when
I quick watch stderr, I see the message in stderr->_base member.
What am I doing wrong? Thanks.
Well, for starters, you're asking in a group full of useless twats, who
will give you a whole big song-and-dance about how your post is:
Off topic. Not portable. Cant discuss it here. Blah, blah, blah.
Which all boils down to "We know the answer, but we're not gonna tell ya
- nanny, nanny, boo boo". Note that it is not the case that your
question actually *is* off-topic; it's just the jerks here, for reasons
that are pretty much rooted in religion (i.e., fantasy), refuse to talk
about it. So, their advice (ask somewhere else) is actually correct,
but not for the reasons that they state. There are, of course,
exceptions to this rule (that no one here will help you), but you will
quickly find that those who do deign to actually help, are quickly
branded as "trolls". I'm one of them. There are others.
Having said all that, let me address your issue. Yes, your problem is
common, well-defined, and solvable. I had to do just that some time
ago; I had a library that was generating output on both stdout and
stderr, and I wanted to capture that output, without going the kludge
route of letting it write out to a file and reading the file back in. (*)
Now, I won't go into the full details (it is much more fun to work it
out on your own), but the basic method is to use some variation of the
setbuf() call to create a large buffer (large enough to contain as much
as output as you ever expect to get - i.e., so that it never actually
gets written out) for the stream(s) in question. Then, after the
library routine returns, you can poke around in the buffer and get out
what you need. Yes, this requires poking around inside the guts of the
"FILE *" structure. Again, despite what the useless twats here tell you,
you won't hurt yourself by doing this. Nor will you go blind...
(*) Note that some of the useless twats here will (and in fact, already
have) stated that this is the only "portable" way to do it. This
obsession with "the only portable way" is one of the many attributes
that marks them as useless twats.