Jak nie działa, jak działa :) Niezależnie od tego czy wpisuję z zastosowaniem rzutowania czy bez. Poniżej działający przykład, ale zanim to jeszcze pytanie. Wiesz, że jak zrobisz tak, to nic nie zobaczysz?
std::cout << '\0' << std::endl;
Obiecany przykład. Uruchom na godbolt.org
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
std::string file_name = "test.bin";
// Write
{
std::ofstream file(file_name, std::ios::binary);
for (unsigned int i = 0; i < 256; ++i)
{
unsigned char c = static_cast<unsigned char>(i);
file.write(reinterpret_cast<char *>(&c), sizeof(c));
}
}
// Read and print
{
std::ifstream file(file_name, std::ios::binary);
std::cout << std::setfill('0');
unsigned char c;
while (file.read(reinterpret_cast<char *>(&c), sizeof(c)))
{
std::cout << "0x" << std::hex << std::setw(2)
<< static_cast<unsigned int>(c) << std::endl;
}
}
}