需要包含的头文件:
#include <iostream>
#include <sstream>
#include <fstream>
函数如下:
std::string LoadFile(const char* filepath)
{
std::string content;
std::ifstream file_stream;
file_stream.exceptions(std::ifstream::failbit | std::ifstream::badbit);
try
{
file_stream.open(filepath);
std::stringstream string_stream;
string_stream << file_stream.rdbuf();
file_stream.close();
content = string_stream.str();
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
return content;
}