1. IO类层次结构
![](https://images2018.cnblogs.com/blog/1401565/201805/1401565-20180528092226695-1393777341.gif)
2. demo
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
void test_fstream();
void test_stringstream();
int main()
{
test_fstream();
test_stringstream();
return 0;
}
void test_fstream()
{
fstream f;
f.open("test_fstream.txt", fstream::in | fstream::out | fstream::binary | fstream::app);
//f<<"hello world"<<endl;
//f<<"i love you"<<endl;
//f.seekg(0,f.beg);
f.seekg(0,f.end);
int length=f.tellg();
cout<<"length is "<<length<<endl;
f.seekg(0,f.beg);
char buf[1024*8];
f.read(buf,length-5);
buf[length]=0;
cout<<buf<<endl;
f.close();
}
void test_stringstream()
{
stringstream ss;
ss<<"hello world"<<endl;
cout<<ss.str();
cout<<ss.str();
char buf[1024*8];
ss.read(buf,5);
buf[5]=0;
cout<<buf<<endl;
cout<<ss.str();
cout<<ss<<endl;
stringstream oss;
oss<<ss;
cout<<"oss"<<oss.str();
}