How to see all bytes of a raw image - c++

I am working with images in c++, i have a program that capture a image the result data is this
then when a write the image in other folder I get this result:
CFileException ex;
CFile file;
file.Open((LPCTSTR)"test", CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate, &ex);
file.Write(image.puc_image, lenOfImage)
when I open the file with the note.
so basically I want to manipulate the data that a write and convert to base 64 and then send to an API.
but I never see all the data when a read the file. so the convention of base64 don't output correctly the data.

Please try to use CFile::typeBinary
If the file is written in text mode, newlines could get mangled.

Related

C++ - Missing end of line characters in file read

I am using the C++ streams to read in a bunch of files in a directory and then write them to another directory. Since these files may be of different types, I am using a the generic ios::binary flag when reading/writing these files.
Example code below:
std::fstream inf( "ex.txt", std::ios::in | std::ios::binary);
char c;
while( inf >> c ) {
// writing to another file in binary format
}
The issue I have is that in the case of files containing text, the end of line characters in these text files are not being written to the output file.
Edit: Or at least they do not appear to be as when the newly written file is opened, there is only a single continuous line of characters.
Edit again: The problem (of the continuous string) appears to persist even when the read / write is made in text mode.
Thus, I was wondering if there was a way to check if a file has text or binary and then read/write it appropriately. Else, is there any way to preserve the end of line characters even when opening the file in binary format?
Edit: I am using the g++ 4.8.2 compiler
When you want to manipulate bytes, you need to use read and write methods, not >> << operators.
You can get the intended behavior with inp.flags(inp.flags() & ~std::ios_base::skipws);, though.

Looking for data in EXIF format

I got the problem with my program made for downloading the DateTimeOrginal data from the .JPG file. I found the document about it on the internet:
https://ExifTool.org/TagNames/EXIF.html
I see that the data I'm looking for is at 0x9003 address.
So right now what I'm trying to do is:
temp = fopen(name, "rb");
open the file binary
fseek (temp, 0x9003, SEEK_SET);
move the File pointer to the address
fscanf(temp, "%s", str);
and load the data to the char[] structure.
Is atleast any of that correct? I'm still thinking that i got the problem with the address, because after compile that program i see only some trash from the file.
The EXIF data is embedded into the jpeg tag APP1 (0xE1).
The first thing to do is to find the jpef tag 0xE1 in the stream; you have to scan all the jpeg tags (marked by 0xFF+tag, in your case 0xFF,0xE1). After you get the tag, find its length by reading the next 2 bytes (and adjust for high endian), then get the tag's content.
After you get the tag's content, then look in it for the EXIF tag you are interested in (0x9003).
The method readStream in the jpeg class of the open source project Imebra gives you an example on how to parse jpeg tags: https://bitbucket.org/binarno/imebra/src/2eb33b2170e76b5ad2737d1c2d81c1dcaccd19e5/project_files/library/imebra/src/jpegCodec.cpp?at=default#cl-867
Given the style of programming of the OP, I'd recommend Easyexif at https://github.com/mayanklahiri/easyexif
It's relatively easy to integrate. Note that fseek() goes to a file position; it does not search for a certain number.

C++ ofstream output to image file writes different data on Windows

Im doing a simple thing: writing the data of an image file stored as a string into the image file containing that string.
std::ofstream f("image.jpeg");
f << image_data; // image_data was created using python and copied over, in hex and turned back into ascii
And yet, the unexpected happens:
becomes:
I cannot understand why this is happening.
When I use python2.7 to get the data from the original picture and write
it to a new file, it works fine.
When I compile and run my program in ubuntu, the picture comes out
fine.
When I write a large text file (larger than the image) into a .txt,
the file comes out fine.
It is only jpegs on Windows that fails. The original image I tried was
an image from a PGP key packet, which came out with half of the
person's head clear and the other half messed up.
The compiled program doesnt mess up all of the data, since like I said above, some of the original picture is shown. Also, the images are the same size, so the jpeg format was preserved at least.
What is happening? I am using ming2 4.7.2 in Code::Blocks on Windows 7. Is Windows just being crazy?
You must open the file in binary mode:
std::ofstream f("image.jpeg", std::ios::out | std::ios::binary);
// ^^^^^^^^^^^^^^^^

File handling C++ , opening in ios::app mode, file getting wiped

I am making a programme in turbo C++ which requires input and output to a file as well as retrieving the file data for later use.
fstream file("playlist.txt",ios::in|ios::app);
this is what I use but everytime i close the programme and run it again, all the previous contents get wiped. I thought ios::app "Sets the stream's position indicator to the end of the stream before each output operation".
I tried ios::nocreate also, but to no effect.
You should have fstream::out too:
fstream file("playlist.txt", fstream::in | fstream::out);
Then you can move file pointer to end of file. in and app can't be used together: you need to open for R/W because app implies you won't read.

Why does the C++ ofstream write() method modify my raw data?

I have a jpeg image in a char[] buffer in memory, all I need to do is write it out to disk exactly as is. Right now I'm doing this
ofstream ofs;
ofs.open(filename);
ofs.write(buffer, bufferLen);
ofs.close();
but the image doesn't come out right, it looks garbled with random black and white stripes everywhere. After comparing the image with the original in a hex viewer, I found out that the ofstream is modifying the data when it thinks I'm writing a newline character. Anyplace that 0x0A shows up in the original, the ofstream writes as two bytes: 0x0D0A. I have to assume the ofstream is intending to convert from LF only to CRLF, is there a standard way to get it to not do this?
Set the mode to binary when you open the file:
http://www.cplusplus.com/reference/iostream/ofstream/ofstream/
You should set the file mode to binary when you are opening it:
std::ofstream file;
file.open("filename.jpg", std::ios_base::out | std::ios_base::binary);
This way the stream doesn't try to adjust the newlines to your native text format.
Try opening the ofstream as binary.
Something like this should work:
ofstream ofs;
ofs.open(filename, ios::out | ios::binary);
ofs.write(buffer, bufferLen);
ofs.close();
Since you are not opening the file in binary mode, it is set to formatted output by default. In formatted output, your implementation performs conversion of the end-of-line characters as you describe.
I wish I could get my version to write anything at all... no errors, no complaints, nothing wrong when you debug it but it doesn't even try and create the file.
What the hell was wrong with fopen, fwrite and fclose... I never had a problem with them