如下所示:
#include <string>#include <fstream>#include <sstream>#include <iostream> #include <stdlib.h>using namespace std; //從文件讀入到string里string readFileIntoString(char * filename){ifstream ifile(filename);//將文件讀入到ostringstream對象buf中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中的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答