📄 c的底层函数实现.cpp
字号:
//需要用的C的底层函数实现:login ,dir,create,delete,open,close,read,write等功能。
#include<fstream.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
class employee
{
public:
char empname[20];
char empid[20];
char age[20];
char salary[20];
char dept[20];
char post[20];
public:
void get();
};
void employee::get()
{
cout<<"enter employee name\n";
cin>>empname;
cout<<"enter employee id\n";
cin>>empid;
cout<<"enter employee age \n";
cin>>age;
cout<<"enter employee salary\n";
cin>>salary;
cout<<"enter employee department \n";
cin>>dept;
cout<<"enter employee post \n";
cin>>post;
}
void menu()
{
int i,j,k,flag=0,choice;
void space();
void create();
void read();
void copy();
void search();
void append();
void deletion();
clrscr();
cout<<"\n\n\n";
for(i=0;i<80;i++)
cout<<"*";
for(i=1;i<=3;i++)
{
cout<<"\n";
for(j=0;j<80;j++)
{
if(j<29||j>51)
cout<<"*";
else
{
if(i==2&&flag==0)
{
cout<<" Employees Database ";
flag=1;
for(k=1;k<=28;k++)
cout<<"*";
break;
}
else
cout<<" ";
}
}
}
cout<<"\n";
for(i=0;i<80;i++)
cout<<"*";
cout<<"\n\n\n\n\n\n";
space();
for(j=1;j<=22;j++)
cout<<"-";
cout<<"\n";
space();
cout<<"| (1). Create file |";
cout<<"\n";
space();
cout<<"| |";
cout<<"\n";
space();
cout<<"| (2). Read data |";
cout<<"\n";
space();
cout<<"| |";
cout<<"\n";
space();
cout<<"| (3). Copy data |";
cout<<"\n";
space();
cout<<"| |";
cout<<"\n";
space();
cout<<"| (4). Search record |";
cout<<"\n";
space();
cout<<"| |";
cout<<"\n";
space();
cout<<"| (5). Append data |";
cout<<"\n";
space();
cout<<"| |";
cout<<"\n";
space();
cout<<"| (6). Delete record |";
cout<<"\n";
space();
cout<<"| |";
cout<<"\n";
space();
cout<<"| (7). Exit |";
cout<<"\n";
space();
for(j=1;j<=22;j++)
cout<<"-";
cout<<"\n\n\n\n\n";
space();
cout<<"\b\b\b\b\b\b\b\bInput the number to indicate choice: ";
cin>>choice;
if(choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6&&choice!=7)
{
cout<<"\n\n\n\n";
space();
cout<<"Invalid choice";
getch();
menu();
}
switch (choice)
{
case 1:
create();
break;
case 2:
read();
break;
case 3:
copy();
break;
case 4:
search();
break;
case 5:
append();
break;
case 6:
deletion();
break;
case 7:
exit(0);
break;
}
}
void space()
{
int i;
for(i=1;i<=30;i++)
cout<<" ";
}
void create()//create function
{
char ch='y';
employee e;
ofstream outfile;
char fname[20];
clrscr();
cout<<"Enter the name of file to create the records \n";
cin>>fname;
outfile.open(fname,ios::out|ios::binary);
employee f;
strcpy(f.empname,"EName:");
strcpy(f.empid,"EmpID:");
strcpy(f.age,"Age:");
strcpy(f.salary,"Salary:");
strcpy(f.dept,"Dept:");
strcpy(f.post,"Post:");
outfile.write((char *)&f,sizeof(f));
outfile.close();
outfile.open(fname,ios::app|ios::binary);
while(ch=='y')
{
cout<<"enter a record\n";
e.get();
outfile.write((char *)&e,sizeof(e));
cout<<endl<<"add another record: Y/N: \n";
cin>>ch;
}
outfile.close();
cout<<"\n records entered successfully";
getch();
menu();
}
void read()//read function
{
employee e;
ifstream infile;
char fname[20];
clrscr();
cout<<"Enter the name of file to display the records \n";
cin>>fname;
infile.open(fname,ios::in|ios::binary);
cout<<"the records are: \n";
while(infile.read((char *)&e,sizeof(e)))
{
cout<<endl<<e.empname<<"\t"<<e.empid<<"\t"<<e.age<<"\t"<<e.salary<<"\t"<<e.dept<<"\t"<<e.post<<endl;
}
getch();
menu();
}
void append()//append function
{
char ch='y';
employee e;
ofstream outfile;
char fname[20];
clrscr();
cout<<"enter the name of file to append the records \n";
cin>>fname;
outfile.open(fname,ios::app|ios::binary);
while(ch=='y')
{
cout<<"enter new record to append\n";
e.get();
outfile.write((char *)&e,sizeof(e));
cout<<endl<<"add another record: Y/N: \n";
cin>>ch;
}
outfile.close();
cout<<"\n records entered successfully";
getch();
menu();
}
void copy()//filecopy function
{
char source[20],target[20];
employee e;
clrscr();
cout<<"enter source filename\n";
cin>>source;
cout<<"enter target filename\n";
cin>>target;
ifstream in;
ofstream out;
in.open(source,ios::in|ios::binary);
out.open(target,ios::out|ios::binary);
while(in.read((char *)&e,sizeof(e)))
{
out.write((char *)&e,sizeof(e));
}
in.close();
out.close();
cout<<"the records are copied successfully \n";
getch();
menu();
}
void deletion()//delete the record depending on the empname
{
employee e;
ifstream infile;
ofstream outfile;
char fname[20],name[20];
int flag=0;
clrscr();
cout<<"Enter the name of file to delete the records \n";
cin>>fname;
cout<<"Enter the employee name to delete \n";
cin>>name;
infile.open(fname,ios::in|ios::binary);
outfile.open("temp.txt",ios::out|ios::binary);
while(infile.read((char *)&e,sizeof(e)))
{
if(strcmp(name,e.empname)!=0)
{
outfile.write((char *)&e,sizeof(e));
}
else
{
flag++;
cout<<endl<<"Deleted the record:"<<endl<<e.empname<<"\t"<<e.empid<<"\t"<<e.age<<"\t"<<e.salary<<"\t"<<e.dept<<"\t"<<e.post<<endl;
}
}
if(infile.eof()&&flag==0)
{
cout<<"No record matched";
}
infile.close();
outfile.close();
ifstream in;
ofstream out;
in.open("temp.txt",ios::in|ios::binary);
out.open(fname,ios::out|ios::binary);
while(in.read((char *)&e,sizeof(e)))
{
out.write((char *)&e,sizeof(e));
}
in.close();
out.close();
getch();
menu();
}
void search_name()//search function by empname
{
employee e;
ifstream infile;
char name[20],fname[20];
clrscr();
cout<<"Enter the name of file to search the records \n";
cin>>fname;
infile.open(fname,ios::in|ios::binary);
cout<<"enter the employee name to search \n";
cin>>name;
int flag=0;
while(infile.read((char *)&e,sizeof(e)))
{
if(strcmp(name,e.empname)==0)
{
cout<<endl<<e.empname<<"\t"<<e.empid<<"\t"<<e.age<<"\t"<<e.salary<<"\t"<<e.dept<<"\t"<<e.post<<endl;
flag++;
//cout<<flag;
}
}
if(infile.eof()&&flag==0)
{
cout<<"no records found";
}
infile.close();
getch();
menu();
}
void search_id()//search function by empname
{
employee e;
ifstream infile;
int flag=0;
char fname[20],id[20];
clrscr();
cout<<"Enter the name of file to search the records \n";
cin>>fname;
infile.open(fname,ios::in|ios::binary);
cout<<"enter the employee id to search \n";
cin>>id;
while(infile.read((char *)&e,sizeof(e)))
{
if(strcmp(id,e.empid)==0)
{
cout<<endl<<e.empname<<"\t"<<e.empid<<"\t"<<e.age<<"\t"<<e.salary<<"\t"<<e.dept<<"\t"<<e.post<<endl;
flag++;
//cout<<flag;
}
}
if(infile.eof()&&flag==0)
{
cout<<"no records found";
}
infile.close();
getch();
menu();
}
void search()//search function
{
int num;
clrscr();
cout<<"Enter the number to select the search function"<<endl<<"\"1\" for searching by employee name"<<endl<<"\"2\" for searching by employee id"<<endl;
cin>>num;
if(num==1)
{
search_name();
}
if(num==2)
{
search_id();
}
if(num!=1&&num!=2)
{
cout<<"Invalid Number"<<endl<<"Try again";
getch();
search();
}
getch();
}
void main()
{
menu();
getch();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -