(一)C++标准库之IO库

1. IO类层次结构

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();
}
posted @ 2018-05-28 09:25  yvhqbat  阅读(194)  评论(0编辑  收藏  举报