如何使用fstream類在讀寫操作之間任意轉化?

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

using namespace std;

int main()
{
 fstream io("test.txt",ios::in|ios::out|ios::trunc);
 io << "abcdefg" ;
 
 //cout << io.tellp() << endl; 
 io.seekp(0, ios_base::beg);    // <---- 這句話是一定要的!
 //cout << io.tellp() << endl;

 string str1; 
 io >> str1;
 io.close();
 
 cout<<"Text:"<<str1<<endl;
 
 return 0;
}

Copyright@戴維 2006.4  於北京

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