#include <fstream>
#include <cstdint> // For uint8_t
#include <bitset>
#include <iostream>
int main() {
// write
std::ofstream outFile("data.bin", std::ios::binary); // Open file in binary mode
if (!outFile) {
return 1; // Handle error
}
uint8_t bytewrite = 0b10101110; // Example 8-bit data
outFile.write(reinterpret_cast<const char*>(&bytewrite), sizeof(bytewrite)); // Write 1 byte
outFile.close();
// read
std::ifstream inFile("data.bin", std::ios::binary); // Open file in binary mode
if (!inFile) {
return 1; // Handle error
}
uint8_t byteread;
inFile.read(reinterpret_cast<char*>(&byteread), sizeof(byteread)); // Read 1 byte
inFile.close();
std::cout << "Read byte: " << std::bitset<8>(byteread) << std::endl; // Display as binary
return 0;
}
/*
run
Read byte: 10101110
*/