#include #include #include #include #include std::string file_to_string(std::string filename){ int fd = open(filename.c_str(), O_RDONLY); if(fd == -1){ throw std::invalid_argument("File could not be opened"); } struct stat statbuf; fstat(fd, &statbuf); char *contents = (char*)mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); std::string retval(contents); close(fd); munmap(contents, statbuf.st_size); return retval; } using namespace std; int main(){ try{ cout << file_to_string("file_into_string.cpp") << endl; } catch (exception &e){ cout << "Bad File\n"; } return 0; }