0

I'm doing a sqlite insert and I've come across the following code:

sqlite3 *db;
char* error_msg = 0;
int rc = sqlite3_open("New.db", &db);
if (rc) {
    fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
    return(0);
} else {
    fprintf(stderr, "Opened database successfully\n");
}

What exactly is the fprintf doing here? What does stderr mean and do? And finally, if it's writing the output to a file, where can I see that?

Joshua
  • 34,237
  • 6
  • 59
  • 120

1 Answers1

0

When dealing with programs that call other programs in pipeline, we find it convenient to separate normal output from error output. When I run

find . -type f -name '*.c' -print0 | xargs -0 egrep '[a-zA-Z].*somefunction('

I don't want my error messages about can't open directory because the disk is failing going into the pipeline and getting processed by xargs. I want them on the screen.

So programs are given two output channels, one for normal output and one for error output. stderr is how the error output is accessed from a C program.

With programs started from rc.local or otherwise background jobs at boot it's possible to end up with stderr connected to /dev/null instead. This is a problem with the script that started it, and the script needs to be corrected.

Joshua
  • 34,237
  • 6
  • 59
  • 120