⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 程序14.12:文件的打开和关闭.cpp

📁 学习C++的一些范例
💻 CPP
字号:
/* 程序14.12:文件的打开和关闭.cpp:*/
#include<iostream>			//包含头文件
#include<fstream>			//包含头文件
using namespace std;		//使用名字空间std

class Customer				//定义一个类
{
private:
	char cName[25];			//客户姓名
	char cMobileNo[12];		//客户手机
	char cPhoneNo[11];		//客户电话
	char cCity[25];			//客户城市
	char cAddress[50];		//客户地址
	char cBirthday[12];		//客户生日
	float fAmount;			//数量
public:
	void Accept()			//初始化变量
	{
		cout<<"姓名     : ";
		cin.ignore();
		cin.getline(cName,25);
		cout<<"手机号码 : ";
		cin>>cMobileNo;
		cout<<"电话     : ";
		cin>>cPhoneNo;
		cout<<"城市     : ";
		cin.ignore();
		cin.getline(cCity,25);
		cout<<"地址     : ";
		cin.ignore();
		cin.getline(cAddress,50);
		cout<<"出生日期 : ";
		cin>>cBirthday;
		cout<<"数量     : ";
		cin>>fAmount;
	}
	void Display()		//显示置变量
	{
		cout<<"名称     : "<<cName<<endl;
		cout<<"手机号码 : "<<cMobileNo<<endl;
		cout<<"电话     : "<<cPhoneNo<<endl;
		cout<<"城市     : "<<cCity<<endl;
		cout<<"地址     : "<<cAddress<<endl;
		cout<<"出生日期 : "<<cBirthday<<endl;
		cout<<"数量     : "<<fAmount<<endl;
	}
};
int main()
{
	int ch;
	Customer customer;
	while(1)
	{
		cout<<"****** 客户跟踪系统 ******"<<endl;
		cout<<" 0. 退出跟踪系统 "<<endl;
		cout<<" 1. 输入客户资料 "<<endl;
		cout<<" 2. 显示所有记录 "<<endl;
		cout<<"输入你的选择(0-2) : ";
		cin>>ch;
		if(ch==1)
		{
			ofstream outObj("customer.dat");
			char reply='y';
			while(reply=='y'||reply=='Y')
			{
				cout<<"****** 输入客户资料 ******"<<endl;
				customer.Accept();
				outObj.write((char*)&customer,sizeof(customer));
				cout<<"\n你希望继续输入下一个客户资料吗(y/n)?: ";
				cin>>reply;
			}
			outObj.close();
		}
		if(ch==2)
		{
			ifstream inObj("customer.dat");
			cout<<"****** 显示所有记录 ******"<<endl;
			inObj.read((char*)&customer,sizeof(customer));
			while(inObj)
			{
				customer.Display();
				inObj.read((char*)&customer,sizeof(customer));			
			}
			inObj.close();
		}
		if(ch==0)
			break;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -