Thread: connecting using libpq breaks printf

connecting using libpq breaks printf

From
"Joey Morris"
Date:
This is my first attempt at using libpq, and I'm running across a strange
problem. Here is my bare-bones program:

#include <stdio.h>
#include "libpq-fe.h"

int main(int argc, char **argv) {
  PGconn *conn;
  fprintf(stderr, "connecting\n");
  conn = PQconnectdb("dbname=postgres");
  PQfinish(conn);
  return 0;
}

I expected this program to print "connecting", but in fact I get no output
whatsoever. If I comment out the PQconnectdb and PQfinish lines, I see
"connecting" as expected. What could be going on here?

A few notes:
 - I'm running PostgreSQL 8.3.6 on Windows XP. I used the one-click
installer
   to install.
 - I'm compiling the program with MinGW. I get no compiler warnings or
errors.
 - I can connect to the database just fine using pgAdmin and the
command-line
   client. The database is running on localhost.
 - I've tried adding code to see if PQstatus(conn) == CONNECTION_OK, but
this
   hasn't been useful. Since fprintf() isn't working, I can't display a
message
   showing the result of the comparison.
 - I've tried various combinations of connection options in case there was
an
   issue with the hostname, database name, username, or password. I always
get
   the same result: no output.
 - I've tried printing to stdout and to a file, but neither helped.

Thanks for any help you can provide.

Joey



Re: connecting using libpq breaks printf

From
"Albe Laurenz"
Date:
Joey Morris wrote:
> This is my first attempt at using libpq, and I'm running across a strange
> problem. Here is my bare-bones program:
>
> #include <stdio.h>
> #include "libpq-fe.h"
>
> int main(int argc, char **argv) {
>   PGconn *conn;
>   fprintf(stderr, "connecting\n");
>   conn = PQconnectdb("dbname=postgres");
>   PQfinish(conn);
>   return 0;
> }
>
> I expected this program to print "connecting", but in fact I get no output
> whatsoever. If I comment out the PQconnectdb and PQfinish lines, I see
> "connecting" as expected. What could be going on here?

I tried your program, and it compiles and runs on my MinGW installation
and also writes the output to stderr.

One silly question first: are you sure that you actually call your
executable? You didn't name it "test.exe", did you?

If you have problems printing to stderr, you could write to a log file:
outf = fopen("logfile.txt", "a"); fprintf(outf, "whatever"); fclose(outf);
or something like that.
If that doesn't work, odds are good that your code is not executed at all.

Yours,
Laurenz Albe