c++程序设计中文件输入输出流知识点

一.前言

如上

二.内容

题目
编写程序实现以下功能:
(1)按职工号由小到大的顺序将5个员工的数据(包括号码、姓名、年龄、工资)输出到磁盘文件中保存。
(2)从键盘输入两个员工的数据(职工号大于已有的职工号),增加到文件的末尾。
(3)输出文件中全部职工的数据。
(4)从键盘输入一个号码,在文件中查找有无此职工号,如有则显示此职工是第几个
职工,以及此职工的全部数据。如没有,就输出“无此人”。可以反复多次查询,如果输入
查找的职工号为0,就结束查询。

三.代码

#include<iostream>
#include<fstream>
#include<string>
#include<stdlib.h>
using namespace std;
typedef struct{
	int num;//职工号
	long tel;//号码
	string name;//姓名
	int age;//年龄
	double wage;//工资 
} Staff;
//将5个员工从小到大排序,输出到磁盘文件保存 
void sort_save_file1(Staff staffs[],int n){
	int i;//循环变量
	int j;//循环变量
//	int k;
	Staff temp;
	for(i=0;i<n-1;i++){//用选择法进行排序 
		for(j=i+1;j<n;j++){
			if(staffs[i].num>staffs[j].num){
				temp=staffs[i];
				staffs[i]=staffs[j];
				staffs[j]=temp;		
			}
		}
	} 
	
	ofstream outfile("C:\\Users\\17604\\Desktop\\file.data",ios::out|ios::binary);
	if(!outfile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(i=0;i<5;i++){
		outfile.write((char*)&staffs[i],sizeof(staffs[i]));
	}
	 
	outfile.close();
}

//输入两个员工数据,添加到文件的末尾 
void in_app_file(){
	Staff staffs[2];
	int i;//循环变量
	 
	for(i=0;i<2;i++){
		cout<<"请输入第"<<i+1<<"个要追加的员工的数据:"<<endl;
		cout<<"num:"<<endl; 
		cin>>staffs[i].num;
		cout<<"tel:"<<endl;
		cin>>staffs[i].tel ;
		cout<<"name:"<<endl;
		cin>>staffs[i].name ;
		cout<<"age:"<<endl;
		cin>>staffs[i].age ;
		cout<<"wage:"<<endl;
		cin>>staffs[i].wage ;
		cout<<endl; 
	}
	
	ofstream outfile("C:\\Users\\17604\\Desktop\\file.data",ios::app|ios::binary);
	if(!outfile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	
	outfile.seekp(0,ios::end); //定位到文件末尾 
	for(i=0;i<2;i++){
		outfile.write((char*)&staffs[i],sizeof(staffs[i]));
	} 
	outfile.close();
}

//输出文件中全部职工的数据 
void out_file(){
	Staff staffs[8];
	int i;//循环变量 
	ifstream infile("C:\\Users\\17604\\Desktop\\file.data",ios::in|ios::binary);
	if(!infile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	infile.seekg(0,ios::beg); 
	for(i=0;i<7;i++){
		infile.read((char*)&staffs[i],sizeof(staffs[i]));
	}
	infile.close() ;
	for(i=0;i<7;i++){
		cout<<"NO."<<i+1<<endl;
		cout<<"num: "<<staffs[i].num<<endl;
		cout<<"tel: "<<staffs[i].tel<<endl;
		cout<<"name: "<<staffs[i].name<<endl ;
		cout<<"age: "<<staffs[i].age<<endl ;
		cout<<"wage: "<<staffs[i].wage<<endl ;
		cout<<endl;
	}
} 

void query(char *filename){
	Staff staffs[8];
	int i;//循环变量 
	int key;//要查找的职工号 
	int f;//首部 
	int l;//尾部 
	int mid;//中间 
	bool flag=false;//查找标志位 
	ifstream infile(filename,ios::in|ios::binary);
	if(!infile){
		cerr<<"open error!"<<endl;
		exit(1);
	}
	for(i=0;i<7;i++){
		infile.read((char*)&staffs[i],sizeof(staffs[i]));
	}
	infile.close() ;
	
	cout<<"你要查找第几个职工的信息:(输入0结束查询):"<<endl;
	cin>>key;
	while(key!=0){//输入0结束查询
		//使用二分查找
		f=0;
		l=6;
		while(f<=l){
			mid=(f+l)/2;
			if(staffs[mid].num ==key){
				cout<<"查找成功!"<<endl;
				flag=true;
//				cout<<"NO."<<i+1<<endl;
				cout<<"num: "<<staffs[mid].num<<endl;
				cout<<"tel: "<<staffs[mid].tel<<endl;
				cout<<"name: "<<staffs[mid].name<<endl ;
				cout<<"age: "<<staffs[mid].age<<endl ;
				cout<<"wage: "<<staffs[mid].wage<<endl ;
				cout<<endl;
				break;
			}
			else if(key<staffs[mid].num){
					l=mid-1;
				 } 
				 else{
				 	f=mid+1;
				 }
		} 
		
		if(flag==true){
			
		}
		else{
			cout<<"无此人!!"<<endl;
		}
		flag=false;
		cout<<"你要查找第几个职工的信息:"<<endl;
		cin>>key;
	}
}

int main(){
	Staff staffs[5];
	int i;//循环变量 
	char *p="C:\\Users\\17604\\Desktop\\file.data";
	for(i=0;i<5;i++){
		cout<<"请输入第"<<i+1<<"个员工的数据:"<<endl;
		cout<<"num:"<<endl; 
		cin>>staffs[i].num;
		cout<<"tel:"<<endl;
		cin>>staffs[i].tel ;
		cout<<"name:"<<endl;
		cin>>staffs[i].name ;
		cout<<"age:"<<endl;
		cin>>staffs[i].age ;
		cout<<"wage:"<<endl;
		cin>>staffs[i].wage ;
		cout<<endl; 
	}
	sort_save_file1(staffs,5);
	in_app_file();
	out_file();
	query(p);
	return 0;
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

无言月梧桐

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值