1

I write string to stderr by fprintf(strderr,format strimg, string) and later from other function I need to check if anything was written to stderr.

For this matter, is it ok to just check if stderr is empty? Or maybe ferror(stderr) will do the trick?

qwerty
  • 175
  • 4
  • 15
  • 1
    Afaik you can't check if stderr has been written to in any portable way. Couldn't you just break out the output to stderr to a separate function and set a boolean if it's ever called? – Joachim Isaksson Mar 09 '12 at 13:14

3 Answers3

1

Probably better to use a boolean flag to track whether an error has been given or not - initialise it to false and set it to true any time you write to stderr. Then check that...

JTeagle
  • 2,196
  • 14
  • 15
0

have a look at this

in the comments, there's something about EOF, too. could be exactly what you're looking for?

Community
  • 1
  • 1
sinned
  • 503
  • 1
  • 7
  • 25
0

If you have the chance, do

#include <stdio.h>
#include <stdarg.h>
char errprintf_called = 0;
int errprintf(const char * fmt, ...)
{
    errprintf_called = 1;
    va_list ap;
    va_start(ap, fmt);
    int ret = vfprintf(stderr, ap);
    va_end(ap);
    return ret;
}

and use

errprintf("Error %d has occured!", 42);
if (errprintf_called) {
    whine();
} else {
    laugh();
}
glglgl
  • 89,107
  • 13
  • 149
  • 217