ifstream -> filebuf -> string -> istringstream (= cin)

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

using namespace std;

int main ()
{
    ifstream  mf;
    filebuf  *pbuf;

    char *strbuf;
    mf.open("./test.txt");
    pbuf = mf.rdbuf();

    long len = pbuf->pubseekoff(0, ios::end, ios::in);
    cout << "len: " << len << endl;
    pbuf->pubseekpos(0, ios::in);
    strbuf = new char[len];
    pbuf->sgetn(strbuf, len);

    string str(strbuf);
    cout << "str: " << str << endl;

    istringstream istr(str, istringstream::in);
    cout << "istr: " << istr.str() << endl;

    delete strbuf;

    mf.close();
    return 0;
}

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