c++标准中没有一个api可以加载文件到string类型,网上都说的是先用ostringstream中转下,今发现了boost中有一个这样的api,用着挺好用,注意捕捉异常。
#include<boost/filesystem.hpp>
#include<iostream>
using namespace std;
string strRet;
string path="/root/hello.txt";
try {
boost::filesystem::load_string_file(path.c_str(), strRet);
}
catch (exception &ex) {
cerr << ex.what() << endl;
return;
}
cout << strRet << endl;
相应的写文件的api是
void save_string_file(const path& p, const std::string& str);