Giter Club home page Giter Club logo

simple-cxx-binary-io's Introduction

Simple C++ binary I/O

binary_io.hpp is a header providing sane low-level binary I/O functions in C++. Download and #include it to get access to [try_]read_bytes and [try_]write_bytes. The C++20 standard is required.

read_bytes and try_read_bytes read binary data from std::istreams, write_bytes and try_write_bytes write binary data to std::ostreams.

The try_* methods throw std::ios_base::failure if reading or writing fails.

Methods

bool [try_]read_bytes( [std::istream &s, ]      auto *const data[, const size_t size])
bool [try_]write_bytes([std::ostream &s, ]const auto *const data[, const size_t size])

bool [try_]read_bytes( [std::istream &s, ]      auto *const data...)
bool [try_]write_bytes([std::ostream &s, ]const auto *const data...)

T [try_]read_bytes<T>([istream &s])

Arguments

s: the std::istream from which to read, or the std::ostream to which to write (optional, default std::cin for reading, std::cout for writing)

data: a pointer (or pointers, if size isn't specified) to the element(s) to store the data to be read in, or from which to take data to be written out

size: the number of elements (not bytes) to read in or write out (optional, default 1)

Return Value

Methods returning bool return true if the read was successful, false otherwise. The template version taking a type returns a value of type T.

Examples

Open the file vals.dat and read two values, an int and a float, from it:

int i; float f;
std::ifstream is("vals.dat");
bool read_success = read_bytes(is, &i, &f);

Read a 3x6 array of floats from stdin:

float a[3][6];
bool read_success = read_bytes(&a, 3*6);

Read a size_t from stdin, then read that many floats into a std::vector:

auto n = read_bytes<size_t>();
std::vector<float> v(n);
bool read_success = read_bytes(v.data(), n);

A struct with constructor that reads an int then that number of floats into a vector, throwing if the read fails:

struct MyStruct {
    const int n; // members MUST be arranged in the order that they are read in
    std::vector<float> v;
    MyStruct(std::istream &s): n{try_read_bytes<int>(s)}, v(n) {
        try_read_bytes(s, v.data(), n);
    }
};

Write the contents of std::vector v to the file v.dat, throwing if the write fails:

std::ofstream f("v.dat");
try_write_bytes(f, v.data(), v.size());

Generate 3 random ints and write them to stdout:

int x = rand(), y = rand(), z = rand();
bool write_success = write_bytes(&x, &y, &z);

simple-cxx-binary-io's People

Contributors

mjg0 avatar

Stargazers

Mark Harris avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.