Created
January 22, 2014 12:40
-
-
Save nicolausYes/8558058 to your computer and use it in GitHub Desktop.
Class to read/write files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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