Skip to content

Instantly share code, notes, and snippets.

@nicolausYes
Created January 22, 2014 12:40
Show Gist options
  • Save nicolausYes/8558058 to your computer and use it in GitHub Desktop.
Save nicolausYes/8558058 to your computer and use it in GitHub Desktop.
Class to read/write files
#pragma once
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
class File
{
public:
static bool Read( const std::string& file, std::vector< unsigned char >& data )
{
std::ifstream in( file, std::ifstream::binary );
if( !in )
return false;
// get length of file
in.seekg( 0, in.end );
std::streamoff length = in.tellg();
in.seekg( 0, in.beg );
data.clear();
data.resize( length );
in.read( (char*)&data[0], length );
in.close();
return true;
}
static bool Write( const std::string& file, std::vector< unsigned char >& data )
{
std::ofstream out( file, std::ios::binary );
if( !out )
return false;
out.write( (const char*) &data[0], data.size() );
out.close();
return true;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment