Skip to content

Instantly share code, notes, and snippets.

@phrz
Created September 21, 2018 17:33
Show Gist options
  • Select an option

  • Save phrz/e621da0e3a5047b0d042d34c2965cf3a to your computer and use it in GitHub Desktop.

Select an option

Save phrz/e621da0e3a5047b0d042d34c2965cf3a to your computer and use it in GitHub Desktop.
Helpers for reading binary file formats in C++.
#include <fstream>
#include <iostream>
#include <array>
// creates a new instance of the type
// and reads data into it. Good for single
// value types.
template<typename T>
T STLParser::readBinary(std::ifstream& ifs) {
T out;
ifs.read(
reinterpret_cast<char*>(&out),
sizeof(T)
);
return out;
}
// reads a series of binary values
// into an array with a given type.
// Should be size and type safe.
template<typename T, size_t size>
void readBinaryArray(
std::ifstream& ifs,
std::array<T,size>& data
) {
ifs.read(
reinterpret_cast<char*>(&data),
sizeof(T) * size
);
}
int main() {
std::string fileName = "sphere.stl";
std::ifstream file { fileName, std::ios::binary };
if(!file.is_open()) {
std::cerr << "Could not open file." << std::endl;
return 1;
}
// Skipping header (80 bytes)
for(int i = 0; i < 80; i++) {
file.get();
}
auto triangleCount = readBinary<uint32_t>(file);
std::array<float, 12> points;
for(uint32_t i = 0; i < triangleCount; i++) {
// reads 12 floats into `points`
readBinaryArray(file, points);
// skip the attribute data
readBinary<uint16_t>(file);
}
}
@phrz
Copy link
Copy Markdown
Author

phrz commented Oct 12, 2019

Hey it’s no problem. To be clear the intent of this code is to parse binary STL files which follow a consistent, well defined encoding - it’s not to deserialize data serialized by this or other C++ implementations. I would not recommend this code for such a broad purpose, but instead a more robust serialization library, perhaps protobufs or bson.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment