I'm trying load a executable file (.exe) in a vector<char>
but never is loaded complete data, only MZx string. How fix?
vector<char> buffer;
ifstream infile;
infile.open("putty.exe", ios::binary);
infile.seekg(0, ios::end);
size_t file_size_in_byte = infile.tellg();
buffer.resize(file_size_in_byte);
infile.seekg(0, ios::beg);
infile.read(&buffer[0], file_size_in_byte);
infile.close();
cout << buffer.data() << endl;
ofstream ofs("data.bin", ofstream::out);
ofs << buffer.data();
ofs.close();
Given vector<char> buffer
then buffer.data()
returns a char*
. Consequently ofs << buffer.data()
is sending a char pointer to the stream. The <<
overload for a char pointer handles it as a string - a sequence of characters that end with a null terminator. That isn't what you want for handling an arbitrary set of bytes (such as the contents of an .exe file). So your output stops at the first null byte.
You need to send your data to the output stream in a different way. The write function should work. For example:
ofs.write(buffer.data, buffer.size());