Writing binary data from database to file

J

Julia Jacobson

Hello everybody out there using C++,

My C++ program should retrieve binary data from a database and write it
to a file. It looks like this:

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include "libpq-fe.h"
using namespace std;

int main(void)
{
PGconn *conn;
PGresult *res;
conn = PQconnectdb("hostaddr='my.remote.host' port='5432'
dbname='my_db' user='user' password='secret' connect_timeout='9'");
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
res = PQexec(conn, "SELECT bindat FROM my_tab WHERE bindat_id='3'");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "Error: %s", PQerrorMessage(conn));
fprintf(stderr, "Error: %s", PQresultErrorMessage(res));
PQclear(res);
PQfinish(conn);
}
char buffer[100];
ofstream myfile ("data.bin", ios::eek:ut | ios::binary);
buffer = PQgetvalue(res, 0, 0);
myfile.write(buffer, 100);
}
myfile.close();
}

Compilation with "g++ -I C:\Programs\PostgreSQL\8.3\include -L
C:\Programs\PostgreSQL\8.3\bin -lpq my_prog.cpp" returns the error message:
my_prog.cpp: In function 'int main()':
my_prog.cpp:29: error: incompatible types in assignment of 'char*' to
'char [100]'
my_prog.cpp: At global scope:
my_prog.cpp:32: error: expected constructor, destructor, or type
conversion before '.' token
my_prog.cpp:33: error: expected declaration befor '}' token

I'm using g++ (GCC) 4.2.1-sjlj (mingw32-2) on Windows XP.
Could anyone help me to finde a way to correct my program?

Thanks in advance,
Julia
 
A

Alf P. Steinbach /Usenet

* Julia Jacobson, on 04.09.2010 00:55:
Hello everybody out there using C++,

My C++ program should retrieve binary data from a database and write it to a
file. It looks like this:

#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <fstream>
#include "libpq-fe.h"
using namespace std;

int main(void)
{
PGconn *conn;
PGresult *res;
conn = PQconnectdb("hostaddr='my.remote.host' port='5432' dbname='my_db'
user='user' password='secret' connect_timeout='9'");
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s",
PQerrorMessage(conn));
PQfinish(conn);
exit(1);
}
res = PQexec(conn, "SELECT bindat FROM my_tab WHERE bindat_id='3'");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "Error: %s", PQerrorMessage(conn));
fprintf(stderr, "Error: %s", PQresultErrorMessage(res));
PQclear(res);
PQfinish(conn);
}
char buffer[100];
ofstream myfile ("data.bin", ios::eek:ut | ios::binary);
buffer = PQgetvalue(res, 0, 0);
myfile.write(buffer, 100);
}
myfile.close();
}

Compilation with "g++ -I C:\Programs\PostgreSQL\8.3\include -L
C:\Programs\PostgreSQL\8.3\bin -lpq my_prog.cpp" returns the error message:
my_prog.cpp: In function 'int main()':
my_prog.cpp:29: error: incompatible types in assignment of 'char*' to 'char [100]'
my_prog.cpp: At global scope:
my_prog.cpp:32: error: expected constructor, destructor, or type conversion
before '.' token
my_prog.cpp:33: error: expected declaration befor '}' token

I'm using g++ (GCC) 4.2.1-sjlj (mingw32-2) on Windows XP.
Could anyone help me to finde a way to correct my program?

Arrays aren't directly assignable in C or C++.

Most probably PQgetvalue takes as arguments a pointer to an array and an array
size, where you supply 0 and 0.

Check the documentation of PQgetvalue.

By the way the curly braces don't seem to match up.

Very systematic indentation helps to fix that kind of problem.


Cheers & hth.,

- Alf
 
F

Francesco S. Carta

Hello everybody out there using C++,

My C++ program should retrieve binary data from a database and write it
to a file. It looks like this:

Hi Julia, this program cannot be compiled if one who has not PostGreSQL
installed, please refer to:

http://www.parashift.com/c++-faq/

FAQ 5.8 in particular, the next time you'll need help here.

I'll post a couple of notes between the lines of your program:
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
#include <fstream>

The C++ version of those C libraries (stdlib.h and stdio.h) should be
included in this way:

#include <cstdlib>
#include <cstdio>

The rule is: drop the ".h", add a leading "c". You shouldn't actually
include them unless they're needed, and in this case they're needed just
because you're not making full use of C++ - your program is a mix of C
and C++, which is best avoided as much as possible.
#include "libpq-fe.h"
using namespace std;

int main(void)

Make that "int main()" - drop the void, it's not the C++ style, although
it's allowed for compatibility.
{
PGconn *conn;
PGresult *res;
conn = PQconnectdb("hostaddr='my.remote.host' port='5432' dbname='my_db'
user='user' password='secret' connect_timeout='9'");
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn));

Use the standard C++ streams instead of printf() functions.
PQfinish(conn);
exit(1);
}
res = PQexec(conn, "SELECT bindat FROM my_tab WHERE bindat_id='3'");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "Error: %s", PQerrorMessage(conn));
fprintf(stderr, "Error: %s", PQresultErrorMessage(res));
PQclear(res);
PQfinish(conn);
}
char buffer[100];
ofstream myfile ("data.bin", ios::eek:ut | ios::binary);
buffer = PQgetvalue(res, 0, 0);

You cannot assign to an array like this. You'd need to use some function
to fill the buffer with the results of that function call, read the
section about filling buffers in a C or C++ manual or reference.
myfile.write(buffer, 100);
}
myfile.close();
}

Here above you have a closing curly brace in excess - the first one.

Alf's suggestion about proper formatting and indentation is very
important. You might find some code formatter in your editor, abuse it;
if your editor has no code formatter, get a better editor.
Compilation with "g++ -I C:\Programs\PostgreSQL\8.3\include -L
C:\Programs\PostgreSQL\8.3\bin -lpq my_prog.cpp" returns the error message:
my_prog.cpp: In function 'int main()':
my_prog.cpp:29: error: incompatible types in assignment of 'char*' to
'char [100]'
my_prog.cpp: At global scope:
my_prog.cpp:32: error: expected constructor, destructor, or type
conversion before '.' token
my_prog.cpp:33: error: expected declaration befor '}' token

I'm using g++ (GCC) 4.2.1-sjlj (mingw32-2) on Windows XP.
Could anyone help me to finde a way to correct my program?

Hope that helps, remember to check the FAQ I linked to learn about
writing C++ in the C++ style, have good time learning C++ :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top