最简洁的做法是用ios::rdbuf():
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
bool getFileStr(string filename, string str)
{
ifstream src(filename.c_str());
if(src.is_open())
{
str = src.rdbuf().str();
src.close();
return true;
}
retrun false;
}