Is it possible to disable stderr in C ++?

I wrote a program for linux using libxml2 for parsing html. Although it does its job, the html parser writes many errors to stderr. Is it possible to disable stderr at all (or redirect it to / dev / null, rather than launching it using a redirect shell script)? I can live with the need to write my own errors in stdout, I just want to get rid of these errors.

+5
source share
5 answers

Use freopen to redirect dev / null:

freopen("/dev/null", "w", stderr);
+17
source

freopen() ing stderr , . libxml2, , stderr . , - . . libxml2 , libxml2. xmlSetGenericErrorFunc()

+14

freopen (3) - C- ( ++, ), , . . , 2 /dev/null , 2. . , freopen (3) . , , libxml2 stdio C.

POSIX open (2) dup2 (2):

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

...

/* error checking elided for brevity */
int fd = ::open("/dev/null", O_WRONLY);
::dup2(fd, 2);
::close(fd);
+5

. pipe(2). STDERR /dev/null, .

0

You can redirect stderr (in bash, anyway) from the command line as such:

./myProgram 2> / dev / null

0
source

All Articles