// G. Hagopian working on steganography

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace std;

template<class T>
char* as_bytes(T& i)  {// treat a T as a sequence of bytes
	void* addr = &i; // get the address of the first byte
	// of memory used to store the object
	return static_cast<char*>(addr); // treat that memory as bytes
}

int main() {
	fstream fs("ada-lovelace.png", fstream::in | fstream::out | fstream::binary);
	// read from binary file:
	string message{ "Hagopian was here." };
	string s;
	int i = 0;
	for (char x; fs.read(as_bytes(x), sizeof(char)); ++i) {// note: reading bytes
		/*if (i == 331) {
			fs.write("Zimmerman was here", sizeof(char));
			s += 'P';
		}
		else*/
		s += x;
	}
	fstream fs2("adalovelace.png", fstream::out | fstream::binary);
	//cout << s;
	// write to binary file:
	for (int i = 0; i < s.size(); ++i) {
		if (i == 363008) fs2.write("Hagopian was here", 17);
		else fs2.write(as_bytes(s[i]), sizeof(char)); // note: writing bytes
	}
}