C++试题精选----I/O----NO.4

希望c++的入门者们闲暇之余,可以浏览思考,有什么问题欢迎留言或者私信。
I/O----NO.4(在NO.3的基础上进行扩展)
从键盘上输入学生的QQ号,再由NO.3所建立的通讯录文件中查找该同学的资料。查找成功时,显示其所有通迅信息。
代码演示

#include<iostream>
#include<fstream>//文件流头文件 
#include<string>
#include <iomanip>
using namespace std;
static int n=0;
class student
{
	private:
		string QQ;
		string name;
		string phone;
		string E_mail;
		string location;
	public:
	void setQQ()
	{
		cout<<"输入QQ号:"; 
		cin>>QQ;//输入QQ 
	} 
	void setname()
	{
		cout<<"输入名字:";
		cin>>name;//输入名字 
	}
	void setphone()
	{
		cout<<"输入电话号码:";
		cin>>phone;//输入电话号码 
	}
	void setE_mail()
	{
		cout<<"输入e_mail: ";
		cin>>E_mail;//输入e_mail 
	}
	void setlacation()
	{
		cout<<"输入地点:";
		cin>>location;//输入地点 
	}
	string getQQ()
	{
		return QQ;//得到QQ 
	}
	string getname()
	{
		return name;//得到名字 
	}
	string getphone()
	{
		return phone;//得到电话号码 
	}
	string getE_mail()
	{
		return E_mail;//得到e_mail 
	}
	string getlocation()
	{
		return location;//得到地点 
	}
	void hanshu()
	{
		string _QQ;
		ifstream file("student.dat",ios::binary);
		if(!file)
		{
			cout<<"File cannot be opened"<<endl;
			return;
		}
		student one;
		file.seekg(0);
		cout<<"输入查询QQ:"<<endl;
		cin>>_QQ;
		file.read((char*)&one,sizeof(one));
		while(file)
		{
			if(one.QQ==_QQ)
			cout<<" 邮箱:"<<one.E_mail<<" QQ: "<<one.getQQ() <<" 名字: "<<one.getname() <<" 手机号:"<<one.getphone() <<" 地点: "<<one.getlocation() <<endl; 
			file.read((char*)&one,sizeof(one));
		}
		file.close();
	}
};
int main()
{
	ofstream outfile("student.dat",ios::binary);
	if(!outfile)
	{
		cout<<"File student.dat cannot be opened."<<endl;
		return 0;
	}
	student stud[100];
	char ch;
	int i=0;
	while(1)
	{
		cout<<"\n你想输入更多记录吗?(y/n)?";
		cin>>ch;
		if(ch=='n'||ch=='N')
		break;
		i=i+1; 
		stud[i].setQQ();//输入QQ 
		stud[i].setname();//输入名字 
		stud[i].setphone();//输入电话号码 
		stud[i].setE_mail();//输入电子邮件 
		stud[i].setlacation();//输入地点 
		outfile.write((char*)&stud[i],sizeof(student));
	}
	outfile.close();//关闭文件 
	cout<<"******输入结束******"<<endl;
	cout<<"\n你想查看吗?(y/n)";
	cin>>ch;
	if(ch=='Y'||ch=='y')
	{
		ifstream infile("student.dat",ios::binary);
		if(!infile)
		{
			cout<<"File student.dat cannot be opened."<<endl;
			return 0;
		}
		cout<<"名字"<<"\t"<<setw(20)<<"邮箱"<<"\t"<<setw(20)<<"地点"<<"\t"<<setw(20)<<"QQ"<<"\t"<<setw(20)<<"手机号码"<<endl;
		i=1;
		infile.read((char*)&stud[i],sizeof(student));
		while(infile)
		{
			cout<<stud[i].getname()<<"\t";
			cout<<setw(20)<<stud[i].getE_mail()<<"\t";
			cout<<setw(20)<<stud[i].getlocation()<<"\t";
			cout<<setw(20)<<stud[i].getQQ()<<"\t"; 
			cout<<setw(20)<<stud[i].getphone()<<endl;
			i=i+1;
			infile.read((char*)&stud[i],sizeof(student));
		}
		infile.close();//关闭文件 
	}
	student s;
	s.hanshu();
	return 0;
}

运行结果
在这里插入图片描述
有问题私聊博主或者在下面留言,如果有更好的解法也请留言,欢迎大家讨论,共同进步,一起学习。

C++是面向对象编程,我也想面向对象编程。”

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

敲代码的xiaolang

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值