一次性讀取文件內容

兩種方式

# include<sstream>
# include<fstream>
# include<string>

int main()
{
    ifstream in("input.txt");
    stringstream temp;
    temp<<in.rdbuf();
    string text = temp.str();//將字符串流轉化爲字符串
    in.close();
}

# include<fstream>
# include<string>

int main()
{
    ifstream in("input.txt");
    istreambuf_iterator<char> beg(in), end;//利用以字符爲元素的流迭代器將文件流中所有的字符
    string text(beg, end);//存入到字符串中
    in.close();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章