如下所示:
#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關(guān)聯(lián)的字符串return buf.str();} int main(){//文件名char * fn="a.txt";string str;str=readFileIntoString(fn);cout<<str<<endl;system("pause"); }以上這篇C++ 讀文件 將文件內(nèi)容讀入到字符串string中的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答