I was wondering, how can I read in a file (Any kind of file) as 0s and 1s and store it in an array. Also, how could I write it back as a file, so that in the end I have two identical functional files (Regardless of the type of the file, like .png, .txt, etc)
I tried this:
#include <iostream> // std::cout
#include <fstream> // std::ifstream
int main()
{
std::ifstream is("C:/Test/Test.png", std::ifstream::binary);
if (is) {
// get length of file:
is.seekg(0, is.end);
int length = is.tellg();
is.seekg(0, is.beg);
char* buffer = new char[length];
std::cout << "Reading " << length << " characters... ";
// read data as a block:
is.read(buffer, length);
if (is)
std::cout << "all characters read successfully.";
else
std::cout << "error: only " << is.gcount() << " could be read";
is.close();
// ...buffer contains the entire file...
for (int i = 0; i < length; i++) {
std::cout << buffer[i] << std::endl;
}
delete[] buffer;
}
return 0;
}
but this gives me something like this:
IHDR X X ¾f˜Ü d¢IDATx^ìüÑ庒6ØnOÚ‰6ë¸ÀoD;Ñv”K׉ôÅ<ÙëìÌ‘S
instead of 0s and 1s.