昨天做了一個簡易的職工管理項目,每個功能都能正常運行,但是有個缺陷就是不能長期的保存,運行窗口關閉后所有存儲的數據便會都沒有,即沒有文件操作保存在本地,所以今天對噶項目進行了文件操作的修改,一開始個人還覺得修改的地方不會太多,但仔細考慮后還是有很多注意點和修改的地方:
(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;}
新聞熱點
疑難解答