C++ IO流:iostream、fstream、strstream

#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;
}

你可能感兴趣的:(C++ IO流:iostream、fstream、strstream)