4.输入输出流

1、get系列函数

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	cout << "输入字符串;" << endl;
	getline(cin, str);
	cout << str << endl;

	char sz[60];
	int n = cin.get();
	cin.getline(sz, 60);
	cout << n << endl;
	cout << sz << endl;

}

2、文件读写

#include<iostream>
#include<fstream>
using namespace std;

struct stu
{
	char name[20];
	int grade;
};

int main()
{
	char sz[80];
	fstream in;
	in.open("D:/project2013/STL/4inputoutput/a.txt"); 
	//ifstream in("D:\\project2013\\STL\\a.txt"); 另一种打开方式
	if (!in)
		return 0;
	while (in.getline(sz, 80))
	{
		cout << sz << endl;
	}
	in.close();


	ofstream out;
	out.open("D:/project2013/STL/4inputoutput/b.txt");
	stu st1 = { "xiao", 65 };
	stu st2 = { "gao", 0 };
	out << st1.name << "\t" << st1.grade << endl;
	out << st2.name << "\t" << st2.grade << endl;

	out.write((const char*)&st1, sizeof(stu));
	out << endl;
	out.write((const char*)&st2, sizeof(stu));
	out.close();


	system("pause");
}


3、字符串输入输出流

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

using namespace std;
int main()
{
	int n;
	float f;
	string strHello;
	string strText = "1 3.14 hello";
	istringstream s(strText);
	s >> n;
	s >> f;
	s >> strHello;
	cout << "n=" << n << endl;
	cout << "f=" << f << endl;
	cout << "strHello=" << strHello << endl;

	int i;
	float k;
	string str;
	cout<< "input int float string:";
	cin >> i >> k;
	getline(cin, str);
	ostringstream os;
	os << "int:\t" << i << endl;
	os << "float:\t" << k << endl;
	os << "string:\t" << str << endl;

	string result = os.str();
	cout << result << endl;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值