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: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
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: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