轉載:http://www.cnblogs.com/kex1n/p/4028428.html
第一種方法:
讀取至std::string的情況:
#include <string> #include <fstream> #include <streambuf> std::ifstream t("file.txt"); std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
第二種方法:
#include <string> #include <fstream> #include <sstream>
std::ifstream t("file.txt"); std::stringstream buffer; buffer << t.rdbuf(); std::string contents(buffer.str());