现在的位置: 首页 > 综合 > 正文

C++ IO流:iostream、fstream、strstream

2018年10月07日 ⁄ 综合 ⁄ 共 628字 ⁄ 字号 评论关闭
#include <iostream>
#include <fstream>
#include <strstream>
using namespace std;

int main()
{
	int x,y,z;

	//iostream
	/*
	cin>>x>>y>>z;
	cout<<"x:"<<x<<" y:"<<y<<" z:"<<z<<endl;
	*/

	//fstream
	/*
	ifstream indata;
	indata.open("in.data");
	if(!indata)
	{
		cout<<"fail to open in.data"<<endl;
		return 1;
	}

	indata>>x>>y>>z;

	indata.close();


	ofstream outdata("out.data");
	if(!outdata)
	{
		cout<<"fail to open out.data"<<endl;
		return 1;
	}

	outdata<<x<<","<<y<<","<<z<<endl;

	outdata.close();
	*/

	//strstream
	char srcbuf[] = "123 456 789";
	char dstbuf[20] = {0};

	istrstream indata(srcbuf);
	indata>>x>>y>>z;

	ostrstream outdata(dstbuf, 20, ios::out);
	outdata<<x<<","<<y<<","<<z<<endl;

	printf("dstbuf:%s\n",dstbuf);

	return 0;
}

抱歉!评论已关闭.