Skip to content

Instantly share code, notes, and snippets.

@TyOverby
Last active December 28, 2015 06:59
Show Gist options
  • Select an option

  • Save TyOverby/7461115 to your computer and use it in GitHub Desktop.

Select an option

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.
#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
// 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