今天小编就为大家分享一篇C++ 读文件 将文件内容读入到字符串string中的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
如下所示:
#include #include #include #include #include using namespace std; //从文件读入到string里 string readFileIntoString(char * filename) { ifstream ifile(filename); //将文件读入到ostringstream对象buf中 <b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码网</b>ostringstream buf; char ch; while(buf&&ifile.get(ch)) buf.put(ch); //返回与流对象buf关联的字符串 return buf.str(); } int main() { //文件名 char * fn="a.txt"; string str; str=readFileIntoString(fn); cout<<str<<endl; system("pause"); }
以上就是C++ 读文件 将文件内容读入到字符串string中的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!