|
|
#1 (permalink) |
|
Guest
Posts: n/a
|
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 |
|
|
|
#2 (permalink) |
|
Guest
Posts: n/a
|
* 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: 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 -- blog at <url: http://alfps.wordpress.com> |
|
|
|
#3 (permalink) |
|
Guest
Posts: n/a
|
Julia Jacobson ha scritto:
> [...] > char buffer[100]; > ofstream myfile ("data.bin", ios: ut | ios::binary);> buffer = PQgetvalue(res, 0, 0); > myfile.write(buffer, 100); > } > myfile.close(); > } > > my_prog.cpp:29: error: incompatible types in assignment of 'char*' to > 'char [100]' The problem is what the compiler says it is. You cannot assign a char* (which seems to be what "PQgetvalue" returns) to a char array. How to use the char* returned by "PQgetvalue" (whatever that is) should be stated in the documentation of your DBMS API. Chances are you can use it directly in write(). But you must read the documentation. > 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 Look at the curly braces in your code and where your variables go out of scope... -- Christian Hackl hacki@sbox.tugraz.at Milano 2008/2009 -- L'Italia chiamò, sì! |
|
|
|
#4 (permalink) |
|
Guest
Posts: n/a
|
Julia Jacobson <julia.jacobson@arcor.de>, on 04/09/2010 00:55:22, wrote:
> 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: 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++ :-) -- FSC - http://userscripts.org/scripts/show/59948 http://fscode.altervista.org - http://sardinias.com |
|