Skip to content

Instantly share code, notes, and snippets.

@Nathaniel100
Created May 16, 2016 05:53
Show Gist options
  • Save Nathaniel100/73116736d62ce2da209d12c561e1da8a to your computer and use it in GitHub Desktop.
Save Nathaniel100/73116736d62ce2da209d12c561e1da8a to your computer and use it in GitHub Desktop.
Awesome file operation
inline bool LoadBinaryFile(const char *name, std::vector<uint8_t> *v) {
std::ifstream ifs(name, std::ifstream::binary);
if(!ifs.is_open()) return false;
ifs.seekg(0, std::ios::end);
(*v).resize(static_cast<size_t>(ifs.tellg()));
ifs.seekg(0, std::ios::beg);
ifs.read(reinterpret_cast<char *>(&(*v)[0]), (*v).size());
return !ifs.bad();
}
inline bool SaveBinaryFile(const char *name, const void *data, int size) {
std::ofstream ofs(name, std::ofstream::binary|std::ofstream::trunc);
if(!ofs.is_open()) return false;
ofs.write(reinterpret_cast<const char *>(data), size);
return !ofs.bad();
}
inline bool FileExists(const char *name) {
std::ifstream ifs(name);
return ifs.good();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment