昨天做了一个简易的职工管理项目,每个功能都能正常运行,但是有个缺陷就是不能长期的保存,运行窗口关闭后所有存储的数据便会都没有,即没有文件操作保存在本地,所以今天对噶项目进行了文件操作的修改,一开始个人还觉得修改的地方不会太多,但仔细考虑后还是有很多注意点和修改的地方:
(1)首先就是注册,每注册一个职工信息就需要你利用文件操作存入本地的文件中,这里我用了ofstream outfile ,outfile.open("zhigong.txt",ios::binary | ios::app);outfile.write((char *)&wor,sizeof(wor));outfile.close();有个小注意点就是当往文件里写数据时传递的是一个字符型指针,最后千万不要忘记关掉打开的文件,当保存好了以后就是要查看信息
(2)查看信息在这个项目可以有两个选择,一个是对文件里的内容进行一个一个的读取,将读到的东西打印出来,另外一个则是在你注册的时候将注册好的信息压入到vector数组的最后,然后利用vector<Worker>::iterator定义一个指针对类数组中的元素进行遍历并打印出来,这里我选择了第一个方案,这样更符合文件操作的特征
(3)修改职工的信息;将职工的信息从文件里读取出来很容易,但是想要修改里面的数据就稍微有些复杂了,毕竟要删除原来的信息将现在修改的信息存入到文件中,一开始自己是想着不如先放到vector的类数组中,也就是用指针来接文件里的内容,试了几次后行不通才恍然大悟,存入文件的指针和读取的指针不是同一个类型,没法读取到vector中来,只好另外找办法,也是想了许久才想到可行的方案,就是将原来的文件里内容一个个读取,并存放到另一个文件里去,同时把修改好的信息也放进另一个文件中,删掉最初的文件,将新的文件里的内容复制到和原来文件名(又创建)一样的文件里去,删掉第二个文件,这样做才会在不影响其他功能的前提下解决问题
(4)删除职工的信息:和修改信息一样,利用两个文件来进行操作,将删掉后的所有信息放到第二个文件中,再放到原来的文件里
(5)对职工的薪资进行排序:由于不知道在文件里怎么进行排序,便选择了将文件里的内容全部放到vector里来,再用sort对vector里的成员进行排序,最后用指针遍历所有的元素打印出来,但是这样的操作不会改变文件里的成员排序,毕竟没有对文件的内容进行操作
下面则是自己文件修改后的代码(只有对在上一条博客中的plan.cpp里面进行了操作,其余代码没有改动):
#include "plan.h"
#include<windows.h>
#include <string>
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <time.h>
#include <fstream>
#include <stdlib.h>
using namespace std;
char* mytime()
{
time_t rawtime;
struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
return asctime (timeinfo);
}
bool mysalary(Worker& wor1, Worker& wor2) //比较职工薪资的大小
{
return wor1.salary<wor2.salary;
}
void Plan::jiemian() //主菜单界面
{
system("cls") ;
system("color 5F");
cout<<endl;
cout<<endl;
cout<<"\t\t****************************************************"<<endl;
cout<<"\t\t* "<<mytime() ;
cout<<"\t\t* 请选择您需要的操作! *"<<endl;
cout<<"\t\t* (1)添加职工人员 *"<<endl;
cout<<"\t\t* (2)删除职工人员 *"<<endl;
cout<<"\t\t* (3)修改职工信息 *"<<endl;
cout<<"\t\t* (4)查询 *"<<endl;
cout<<"\t\t* (5)显示所有职工个人信息 *"<<endl;
cout<<"\t\t* (6)按薪资排名 *"<<endl;
cout<<"\t\t* (0)退出 *"<<endl;
cout<<"\t\t* 选择相对的括号里的阿拉伯数字! *"<<endl;
cout<<"\t\t****************************************************";
cout<<endl;
cout<<endl;
return;
}
void Plan::find(std::vector<Worker> &ver) //查询职工信息
{
system("cls");
string stucode;
string stuname;
cout<<"请输入职工工号:";
cin>>stucode;
cout<<endl ;
cout<<"请输入职工姓名:";
cin>>stuname;
ifstream infile ;
infile.open("zhigong.txt",ios::binary);
int temper = 1 ;
while(temper)
{
Worker *p = new Worker ;
if(infile.read((char *)p,sizeof(Worker)))
{
if((p->name == stuname)&&(p->id == stucode))
{
cout<<endl ;
cout<<"职工号:"<<p->id<<"职工姓名:"<<p->name<<" 职工年龄:"<<p->age<<" 性别:"<<p->sex<<" 邮编:"<<p->mail
<<" 部门:"<<p->section<<" 薪资:"<<p->salary<<endl;
free(p) ;
string ii ;
cout<<endl ;
cout<<"返回主界面请按任意键:";
cin>>ii ;
return ;
}
}
else
{
temper = 0 ;
}
free(p) ;
}
cout<<endl ;
cout<<"对不起 未找到您所查找的职工"<<endl ;
Sleep(2000) ;
return;
}
void Plan::list(std::vector<Worker>&ver) //浏览职工个人信息
{
system("cls");
ifstream infile ;
infile.open("zhigong.txt",ios::binary);
int temper = 1 ;
while(temper)
{
Worker *p = new Worker ;
if(infile.read((char *)p,sizeof(Worker)))
{
cout<<"职工号:"<<p->id<<"职工姓名:"<<p->name<<" 职工年龄:"<<p->age<<" 性别:"<<p->sex<<" 邮编:"<<p->mail
<<" 部门:"<<p->section<<" 薪资:"<<p->salary<<endl;
}
else
{
temper = 0 ;
}
free(p) ;
}
cout<<endl<<endl<<endl;
string name;
cout<<"按任意键返回主界面:";
cin>>name ;
return;
}
void Plan::edit(std::vector<Worker> &ver) //修改职工信息
{
system("cls");
string stucode;
string stuname;
cout<<"请输入职工工号:";
cin>>stucode;
cout<<endl ;
cout<<"请输入职工姓名:";
cin>>stuname;
cout<<endl ;
ifstream infile ;
ofstream outfile1;
infile.open("zhigong.txt",ios::binary);
int temper1 = 1 ;
int biaozhi = 0 ;
while(temper1)
{
Worker *p = new Worker ;
if(infile.read((char *)p,sizeof(Worker)))
{
if((p->name == stuname)&&(p->id == stucode))
{
biaozhi = 1 ;
cout<<"原来的信息:"<<endl;
cout<<endl ;
cout<<"职工号:"<<p->id<<"职工姓名:"<<p->name<<" 职工年龄:"<<p->age<<" 性别:"<<p->sex<<" 邮编:"<<p->mail
<<" 部门:"<<p->section<<" 薪资:"<<p->salary<<endl;
cout<<"请输入新信息:"<<endl;
cout<<endl ;
cout<<"请输入姓名:";
cin>>(*p).name;
cout<<endl ;
cout<<"请输入年龄:";
cin>>(*p).age;
cout<<endl ;
cout<<"请输入性别:";
cin>>(*p).sex;
cout<<endl ;
cout<<"请输入职工号:";
cin>>(*p).id;
cout<<endl ;
cout<<"请输入职工邮编:";
cin>>(*p).mail;
cout<<endl ;
cout<<"请输入职工部门:";
cin>>(*p).section;
cout<<endl ;
cout<<"请输入职工薪资:";
cin>>(*p).salary;
cout<<endl ;
cout<<"系统正在存储信息,即将返回主界面......"<<endl;
outfile1.open("beifen.txt",ios::binary | ios::app);
outfile1.write((char *)p,sizeof(Worker));
outfile1.close();
free(p);
continue ;
}
outfile1.open("beifen.txt",ios::binary | ios::app);
outfile1.write((char *)p,sizeof(Worker));
outfile1.close();
}
else
{
if(biaozhi == 0)
{
cout<<"对不起 未找到您所要查询的职工信息,即将返回主界面......";
}
else
{
NULL ;
}
temper1 = 0 ;
}
free(p) ;
}
infile.close() ;
remove("zhigong.txt") ;
ifstream infile1 ;
ofstream outfile2 ;
infile1.open("beifen.txt",ios::binary);
int temper2 = 1 ;
while(temper2)
{
Worker *p = new Worker ;
if(infile1.read((char *)p,sizeof(Worker)))
{
outfile2.open("zhigong.txt",ios::binary | ios::app);
outfile2.write((char *)p,sizeof(Worker));
outfile2.close();
}
else
{
temper2 = 0 ;
}
free(p) ;
}
infile1.close() ;
remove("beifen.txt");
Sleep(3000) ;
return ;
}
void Plan::del(std::vector<Worker> &ver) //删除职工信息
{
system("cls");
string stucode;
string stuname;
cout<<"请输入职工工号:";
cin>>stucode;
cout<<endl ;
cout<<"请输入职工姓名:";
cin>>stuname;
cout<<endl ;
ifstream infile ;
ofstream outfile1;
infile.open("zhigong.txt",ios::binary);
int temper1 = 1 ;
int biaozhi1 = 0 ;
while(temper1)
{
Worker *p = new Worker ;
if(infile.read((char *)p,sizeof(Worker)))
{
if((p->name == stuname)&&(p->id == stucode))
{
biaozhi1 = 1 ;
cout<<endl ;
cout<<"正在删除,请稍等......";
free(p);
continue ;
}
outfile1.open("beifen.txt",ios::binary | ios::app);
outfile1.write((char *)p,sizeof(Worker));
outfile1.close();
}
else
{
if(biaozhi1 == 0)
{
cout<<endl ;
cout<<"对不起 未找到您所要查询的职工信息,即将返回主界面......"<<endl;
}
else
{
NULL;
}
temper1 = 0 ;
}
free(p) ;
}
infile.close() ;
remove("zhigong.txt") ;
ifstream infile1 ;
ofstream outfile2 ;
infile1.open("beifen.txt",ios::binary);
int temper2 = 1 ;
while(temper2)
{
Worker *p = new Worker ;
if(infile1.read((char *)p,sizeof(Worker)))
{
outfile2.open("zhigong.txt",ios::binary | ios::app);
outfile2.write((char *)p,sizeof(Worker));
outfile2.close();
}
else
{
temper2 = 0 ;
}
free(p) ;
}
infile1.close() ;
remove("beifen.txt");
Sleep(2000) ;
return;
}
void Plan::insert(std::vector<Worker> &ver) //添加职工信息
{
system("cls");
Worker wor;
string stucode;
string stuname;
string stusex;
string stuage;
string stumail;
string stusection;
double stusalary;
cout<<"请输入职工工号:";
cin>>stucode;
cout<<endl ;
cout<<"请输入职工姓名:";
cin>>stuname;
cout<<endl ;
cout<<"请输入职工年龄:";
cin>>stuage;
cout<<endl ;
cout<<"请输入职工性别:";
cin>>stusex;
cout<<endl ;
cout<<"请输入职工邮编:";
cin>>stumail;
cout<<endl ;
cout<<"请输入职工部门:";
cin>>stusection;
cout<<endl ;
cout<<"请输入职工薪资:";
cin>>stusalary;
cout<<endl ;
wor.id=stucode;
wor.name=stuname;
wor.age=stuage;
wor.sex=stusex;
wor.mail=stumail;
wor.section=stusection;
wor.salary=stusalary;
//ver.push_back(wor);
ofstream outfile;
outfile.open("zhigong.txt",ios::binary | ios::app);
outfile.write((char *)&wor,sizeof(wor));
outfile.close();
//文件操作
//ofstream outfile("zhigong.txt",ios::out) ;
//if(!outfile)
//{
// cerr<<"打开失败!"<<endl;
// return ;
//}
//outfile<<wor.id<<" ";
//outfile<<wor.name<<" ";
//outfile<<wor.age<<" ";
//outfile<<wor.sex<<" ";
//outfile<<wor.mail<<" ";
//outfile<<wor.section<<" ";
//outfile<<wor.salary<<" ";
//outfile.close();
cout<<"系统正在存储信息,即将返回主界面......"<<endl;
Sleep(3000) ;
return;
}
void Plan::st(std::vector<Worker>&ver) //按职工薪资排名
{
system("cls") ;
ifstream infile ;
infile.open("zhigong.txt",ios::binary);
int temper = 1 ;
while(temper)
{
Worker *p = new Worker ;
if(infile.read((char *)p,sizeof(Worker)))
{
Worker wor ;
wor.id=p->id;
wor.name=p->name;
wor.age=p->age;
wor.sex=p->sex;
wor.mail=p->mail;
wor.section=p->section;
wor.salary=p->salary;
ver.push_back(wor);
}
else
{
temper = 0 ;
}
free(p) ;
}
std::sort(ver.begin(), ver.end(), mysalary);//默认的sort函数是按升序排
vector<Worker>::iterator i;
for(i=ver.begin(); i!=ver.end(); i++)
{
cout<<"职工号:"<<(*i).id<<"职工姓名:"<<(*i).name<<" 职工年龄:"<<(*i).age<<" 性别:"<<(*i).sex<<" 邮编:"<<(*i).mail
<<" 部门:"<<(*i).section<<" 薪资:"<<(*i).salary<<endl;
}
cout<<endl<<endl<<endl;
string name;
cout<<"按任意键返回主界面:";
cin>>name ;
return;
}