Skip to content

Instantly share code, notes, and snippets.

@madhub
Created March 27, 2023 03:15
Show Gist options
  • Save madhub/2132a1d760a3d2c2aea86ed657e8a73e to your computer and use it in GitHub Desktop.
Save madhub/2132a1d760a3d2c2aea86ed657e8a73e to your computer and use it in GitHub Desktop.
C++ samples
/**
 * Read all bytes from a file.
 * 
 * Taken from http://codereview.stackexchange.com/questions/22901/reading-all-bytes-from-a-file
 * 
 * Alternate form: static void ReadAllBytes(char const* filename, std::vector<char>& result)
 */

#include <vector>
#include <fstream>

static std::vector<char> ReadAllBytes(char const* filename)
{
    ifstream ifs(filename, ios::binary|ios::ate);
    ifstream::pos_type pos = ifs.tellg();

    std::vector<char>  result(pos);

    ifs.seekg(0, ios::beg);
    ifs.read(&result[0], pos);

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