Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <vector>
- #include <cstring>
- #include <fstream>
- #include <bit>
- struct Header_t {
- uint32_t field1;
- uint16_t field2;
- uint16_t field3;
- };
- int main() {
- std::ifstream file("abu.bin", std::ios::in | std::ios::binary);
- std::streampos fileSize = 0x12345;
- std::vector<uint8_t> buf(fileSize);
- uint8_t* pBuf = buf.data();
- file.read(reinterpret_cast<char*>(pBuf), fileSize); // (1)
- Header_t header;
- header = *reinterpret_cast<Header_t*>(pBuf); // (2)
- // ^ v одно и тоже
- header = *static_cast<Header_t*>(static_cast<void*>(pBuf)); // (3)
- std::memcpy(&header, pBuf, sizeof(Header_t)); // (4)
- header = *std::bit_cast<Header_t*>(pBuf); // (5)
- static_assert(std::is_trivial_v<Header_t> &&
- std::is_standard_layout_v<Header_t>);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement