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

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);
// ^^^^^^^^^^^^^^^^

Related

How to see all bytes of a raw image

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.

Error while downloading to memory using curl

I want to download a file to memory using curl.
I am currently using this and it looks like it works to a certain extent but corrupts my file.
I have used sigbench and it is around 20% different (while comparing original and downloaded)
The file I want to download is a binary so it won't work after it's modified.
I am currently testing with the x86 version of this.
original binary
downloaded binary
This is the code I am using to write it to a file:
ofstream stream = ofstream("test.dll");
stream.write(chunk.memory, chunk.size);
Opening the file like this:
ofstream stream = ofstream("test.dll");
will cause line-end characters to be adjusted to match your target system.
You should instead open the file in binary mode:
ofstream stream = ofstream("test.dll", std::ios::binary);
This will leave the characters that could be interpreted as line-endings unchanged.
Further reading: https://en.cppreference.com/w/cpp/io/c#Binary_and_text_modes

Saving other files in a own data store with fstream

I am developing at the time a small Filestoresystem, which should store
some files like .png´s and so in it.
,
So I read the bytes from the .png in a char vector successfully, the size of the vector is the same size as the picture (it should be OK).
Then, I wanted to save the bytes in another .png.
Actually, I created the File succesfully, but the File is completely empty.
Here is the most important code, I guess:
void storedFile::saveData(char Path[]){
std::fstream file;
file.open(Path,std::ios::trunc|std::ios::out|std::ios::binary);
if(!file.is_open())
std::cout << "Couldn´t open saved File (In Func saveData())" << std::endl;
file.write((char*)&Data,sizeof(char) * Data.size());
file.close();}
I think that I did it right, but it's not working.
Again, the bytes of the .png are stored in Data.
I tested after every opening and reading, if it opened and so on, everything worked fine (no error codes appeared).
This part looks strange:
file.write((char*)&Data,sizeof(char) * Data.size());
^^^^^^^^^^^^
Data.size() is a hint that data is a std::vector, so &Data is actually wrong, it should be (char*)Data.data()

How to create an uncleared file?

I am a beginner C++ programmer.
I want to create a binary file, that is uncleared with the previous information that was in it. This is easy to do with RAM, simply by making an array, but how do I do this on a hard drive?
How do I create a uncleared file?
In other words how do I retrieve data that was not "cleared" but just marked "empty".
However, if the OS does not allow it, can I launch linux from USB and run my software?
To keep the content of a file to be written on, you can open a file in append mode with:
[ofstream ofs ("filename", ios::binary | ios::app);][1]
All output operations append at the end of the file. Alternatively, you could also use ios::ate so that the output position starts at the end of the file (but afterwards it's up to you).
With the usual read operations you can retrieve preexisting content, by first positionning yourself using seekp().

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.