-2

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.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
Reskareth
  • 47
  • 4
  • `char` or `std::byte` arrays are suitable for this, though if you just want to copy the file you can just ask the OS to do that for you – NathanOliver May 31 '22 at 13:21
  • `fstream` https://www.cplusplus.com/reference/fstream/fstream/ –  May 31 '22 at 13:26
  • If you want to print the `char` array in binary form, see: https://stackoverflow.com/questions/7349689/how-to-print-using-cout-a-number-in-binary-form – NathanOliver May 31 '22 at 13:31
  • 1
    The output you see is the representation of the 0's and 1's in groups of 8 bits (i.e. byte or a `char` in your code). Each byte is printed to stdout as a character. For 0..127 it's simply the ascii character, not sure but I think that 128..255 are implemnetation specific. – wohlstad May 31 '22 at 13:36

1 Answers1

1

You can see below a complete implementation for reading a file, and writing it.

ReadBin will fill a buffer with the binary content of the file.
WriteBin will save a binary buffer to a file.
The 2 files will contain identical content.

#include <string>
#include <vector>
#include <fstream>
#include <assert.h>

bool ReadBin(std::string const & filename, std::vector<char> & data)
{
    std::ifstream file(filename, std::ios::binary | std::ios::ate);
    if (!file.is_open())
        return false;
    auto size = file.tellg();
    file.seekg(0, std::ios::beg);
    data.resize(size);
    file.read((char*)data.data(), size);
    file.close();
    return true;
}

bool WriteBin(std::string const & filename, std::vector<char> const & data)
{
    std::ofstream file(filename, std::ios::binary);
    if (!file.is_open())
        return false;
    file.write(data.data(), data.size());
    file.close();
    return true;
}

int main()
{
    std::vector<char> buf;
    bool bRes = ReadBin("C:\\tmp\\a.png", buf);
    assert(bRes);
    bRes = WriteBin("C:\\tmp\\a_a.png", buf);
    assert(bRes);
    return 0;
}

As for the output you see in the console - see my comment above.

wohlstad
  • 12,661
  • 10
  • 26
  • 39