用类模板写的文件的增删改查操作。
- #ifndef __FILEMA
- #define __FILEMA
- #include<fstream>
- #include<iostream>
- using namespace std;
- struct bookinfo
- {
- char ID[30];
- char name[30];
- char author[30];
- char publishing[30];
- double price;
- int num;
- };
- struct memberinfo
- {
- char name[30];
- char ID[30];
- char address[30];
- };
- struct orderinfo
- {
- char name[30];
- char ID[30];
- char address[30];
- char bookID[30];
- char bookname[30];
- double price;
- int num;
- };
- template<typename T,typename TT>
- class myfile
- {
- public:
- myfile(char *temp);
- long findinfo(int cho,char *cond);
- void delectinfo(long pos);
- void findall();
- long addinfo(T temp);
- long modifyinfo(long pos);
- void outputinfo(long pos);
- private:
- char filename[20];
- };
- template<typename T,typename TT>
- myfile<T,TT>::myfile(char *temp)
- {
- strcpy(filename,temp);
- }
- template<typename T,typename TT>
- long myfile<T,TT>::findinfo(int cho,char *cond)
- {
- //bookinfo temp;
- T temp;
- char tcond[30];
- fstream fbook(filename,ios::out|ios::in|ios::binary);
- strcpy(tcond,cond);
- if (!fbook)
- {
- cout<<"不能打开"<<filename<<endl;
- abort();
- }
- int ii=0;
- while (!fbook.eof())
- {
- fbook.seekp(sizeof(T)*ii);
- fbook.read((char *)&temp,sizeof(T));
- if(1 == cho)
- {
- if(0 == strcmp(temp.name,tcond))
- {
- return fbook.tellg();
- }
- }
- if(2 == cho)
- {
- if(0 == strcmp(temp.ID,tcond))
- {
- return fbook.tellg();
- }
- }
- ii++;
- }
- fbook.close();
- cout<<"没有查找到"<<endl;
- system("pause");
- return 9999; //标志查找失败
- }
- template<typename T,typename TT>
- void myfile<T,TT>::findall()
- {
- T temp;
- fstream fbook(filename,ios::out|ios::in|ios::binary);
- if (!fbook)
- {
- cout<<"不能打开"<<filename<<endl;
- abort();
- }
- int ii=0;
- while (!fbook.eof())
- {
- TT temp2(1);
- fbook.seekp(sizeof(bookinfo)*ii);
- fbook.read((char *)&temp,sizeof(bookinfo));
- if (fbook.eof() !=0 )
- {
- break;
- }
- temp2.display(temp);
- ii++;
- }
- system("pause");
- }
- template<typename T,typename TT>
- void myfile<T,TT>::delectinfo(long pos)
- {
- fstream ftemp("temp.dat",ios::out|ios::in|ios::binary|ios::trunc);
- fstream fbook(filename,ios::out|ios::in|ios::binary);
- T temp;
- int ii=0;
- char info[50];
- while(!fbook.eof())
- {
- fbook.seekp(sizeof(T)*ii);
- fbook.read((char *)&temp,sizeof(T));
- if (pos != fbook.tellg() && !fbook.eof())
- {
- ftemp.write((char *)&temp,sizeof(T));
- TT tt(1);
- tt.display(temp);
- }
- ii++;
- }
- ftemp.close();
- fbook.close();
- system("pause");
- sprintf(info,"del %s",filename);
- // system( "del book.dat"); //删除原始文件
- system(info);
- sprintf(info,"rename temp.dat,%s",filename);
- // system( "rename temp.dat,book.dat"); //修改文件名
- system(info);
- }
- template<typename T,typename TT>
- long myfile<T,TT>::addinfo(T temp)
- {
- fstream fbook(filename,ios::out|ios::in|ios::binary|ios::app);
- fbook.seekg(0,ios::end);
- if (!fbook)
- {
- cout<<"打开"<<filename<<"出错"<<endl;
- abort();
- }
- fbook.write((char *)&temp,sizeof(T));
- fbook.close();
- return fbook.tellg();
- }
- template<typename T,typename TT>
- long myfile<T,TT>::modifyinfo(long pos)
- {
- T temp;
- fstream fbook(filename,ios::out|ios::in|ios::binary);
- TT temp2(1);
- cout<<"请输入要修改的数据"<<endl;
- temp2.setinfo();
- temp2.getstruct(temp);
- fbook.seekp(pos-sizeof(T));
- fbook.write((char *)&temp,sizeof(T));
- fbook.close();
- return pos;
- }
- template<typename T,typename TT>
- void myfile<T,TT>::outputinfo(long pos)
- {
- T temp;
- fstream fbook(filename,ios::out|ios::in|ios::binary);
- fbook.seekp(pos-sizeof(T));
- fbook.read((char *)&temp,sizeof(T));
- TT temp2(1);
- temp2.display(temp);
- fbook.close();
- system("pause");
- }
- #endif