fork download
  1. static std::vector<char> readFile(const char* filename)
  2. {
  3. std::filebuf filebuf;
  4. if(filebuf.open(filename, std::ios::in) == nullptr)
  5. throw std::runtime_error("Cannot open input file.");
  6.  
  7. std::streamoff fileSize;
  8. if((fileSize = filebuf.pubseekoff(0, std::ios::end, std::ios::in)) == -1)
  9. throw std::runtime_error("Cannot get the size of the input file.");
  10.  
  11. const auto bufferSize = fileSize + 1;
  12. if(bufferSize >= std::numeric_limits<std::size_t>::max())
  13. throw std::bad_alloc();
  14.  
  15. std::vector<char> result(static_cast<std::size_t>(bufferSize), '\0');
  16. if(filebuf.sgetn(&result[0], static_cast<std::streamsize>(fileSize)) != fileSize)
  17. throw std::runtime_error("Cannot read the input file.");
  18.  
  19. return result;
  20. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty