In C++ (Visual C++ MFC) i have char *
which is came from database. This is actually a picture. PostgreSQL returns it as char * because no byte[] in C++ (As for as i know -yet-:))
The thing is, i try to write that image like this one:
ofstream myFile ("C:\\picture.jpg", ios::out | ios::binary);
myFile.write(contents, size);
myFile.close();
It output like:
\xffd8ffe000104a46494600010101006000600000ffe1005a4578696600.......
I tried to change contents to reinterpret_cast<char *>(&contents)
then i got few binary data like but just few. The rest of them is not in the file.
I also tried this one:
fstream binary_file("C:\\picture.jpg", ios::out | ios::binary | ios::app);
binary_file.write(reinterpret_cast<char *>(&contents),size);
binary_file.close();
For both with reinterpret_cast<char *>(&contents)
or without it. Still got few byte data in the file. No more.
I also tried to change size
. Size came from postgresql's PQgetlength
method so it is true for sure. (Can't be wrong right?)
I finally give size
myself and said to C++ that it is 5000
. It output binary data with 5.000 but the fact is, it does not meet with original file. It starts with "h" and then something different...
I also tried to load this data to Chilkat's ByteArray and then write with its oen file access method. Still got same result with \xfdd...
.
So, what is the main goal? What am i missing here? Any help will be appreciated.
Edit: It is char *. Sorry for misunderstanding.
Conclusion: I choose Craig Ringer*'s solution due to my needs. But due to this question's nature i choose H2CO3's answer as an accepted answer.