Advertisement
Kitomas

file_read.cpp 2023-12-08

Dec 8th, 2023 (edited)
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <file.hpp>
  2.  
  3. #include <fstream>
  4.  
  5.  
  6.  
  7.  
  8. std::vector<char> file_readBin(const std::string& filePath){
  9.   //open file in binary mode, preemptively seeking to eof
  10.   std::ifstream file(filePath, std::ios::binary | std::ios::ate);
  11.   if(!file.is_open()) throw "File failed to open";
  12.  
  13.  
  14.   std::streamsize fileSize = file.tellg(); //size of file, in bytes
  15.   file.seekg(0, std::ios::beg); //seek to start of file
  16.  
  17.  
  18.   //read file data into a newly-created buffer
  19.   std::vector<char> buffer(static_cast<std::size_t>(fileSize));
  20.   if(!file.read(buffer.data(), fileSize)) throw "File couldn't be read from";
  21.  
  22.  
  23.   return buffer;
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement