Last active
December 28, 2015 06:59
-
-
Save TyOverby/7461115 to your computer and use it in GitHub Desktop.
Handy functions for reading integers out of a file with network byte order. Packaged in a header file for inlining.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef READ_OUT_H_GUARD | |
| #define READ_OUT_H_GUARD | |
| #include <stdint.h> | |
| #include <cstdio> | |
| #include "./fileindexutil.h" | |
| uint64_t read64(FILE* file) { | |
| uint64_t out; | |
| fread(&out, sizeof(out), 1, file); | |
| return ntohll(out); | |
| } | |
| uint32_t read32(FILE* file) { | |
| uint32_t out; | |
| fread(&out, sizeof(out), 1, file); | |
| return ntohl(out); | |
| } | |
| uint16_t read16(FILE* file) { | |
| uint16_t out; | |
| fread(&out, sizeof(out), 1, file); | |
| return ntohs(out); | |
| } | |
| uint8_t read8(FILE* file) { | |
| uint8_t out; | |
| fread(&out, sizeof(out), 1, file); | |
| return out; | |
| } | |
| #endif // READ_OUT_H_GUARD |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // somewhere in your code | |
| uint32_t magic_number = read32(file_); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment