物理删除与逻辑删除有什么区别?
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
static int n=0;
int comp(char*,char*);
class MyFriend
{
char tag;
unsigned int age;
char name[12];
char TelNo[12];
public:
int getMyFriendNo(){return age;}
void getdata()
{
tag='#';
cout<<"(年龄 姓名 电话):";
cin>>age>>name>>TelNo;
}
char gettag(){return tag;}
void disp()
{
if(tag=='#')
cout<<setiosflags(ios::left)
<<setw(6)<<age<<setw(10)<<name<<setw(7)<<TelNo<<endl;
}
char *getname()
{
return name;
}
};
void func1()
{
ofstream output("MyFrd.dat");
MyFriend s;
cout<<"输入数据"<<endl;
cout<<"我的朋友人数:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"第"<<i+1<<"个我的朋友";
s.getdata();
output.write((char *)&s,sizeof(s));
};
output.close();
}
void func2()
{
ifstream input("MyFrd.dat");
MyFriend s;
cout<<"输入数据:"<<endl;
cout<<setiosflags(ios::left)
<<setw(6)<<"年龄"<<setw(10)<<"姓名"<<setw(7)<<"电话"<<endl;
input.read((char *)&s,sizeof(s));
while(input)
{
s.disp();
input.read((char*)&s,sizeof(s));
};
input.close();
}
void func3()
{
char sname[10];
ifstream file("MyFrd.dat");
MyFriend one;
file.seekg(0);
cout<<"输入要查询的姓名(可只输入姓氏):";
cin>>sname;
cout<<"输出查询结果:"<<endl;
cout<<setw(6)<<"年龄"<<setw(10)<<"姓名"<<setw(6)<<"电话"<<endl;
file.read((char *)&one,sizeof(one));
while(file)
{
if(comp(one.getname(),sname)==1)
one.disp();
file.read((char*)&one,sizeof(one));
};
file.close();
}
void func4()
{
fstream outapp("MyFrd.dat",ios::app);
MyFriend one;
cout<<"添加数据:";
one.getdata();
outapp.write((char*)&one,sizeof(one));
outapp.close();
}
void func5()
{
char sname[12] ;int i=0;
fstream outdel("MyFrd.dat",ios::in|ios::out);
MyFriend one;
outdel.seekg(0);
cout<<"输入要删除的姓名(可只输入姓氏):";
cin>>sname;
while(!outdel.eof())
{
outdel.seekp(sizeof(MyFriend)*i);
outdel.read((char *)&one,sizeof(one));
if(comp(one.getname(),sname)==1 && one.gettag()=='#')
{
outdel.seekp(sizeof(MyFriend)*i);
outdel.put('*');
}
i++;
}
outdel.close();
}
void func6()
{
fstream outdel("MyFrd.dat",ios::in);
fstream temp("temp",ios::out|ios::trunc);
MyFriend s;
while(!outdel.eof())
{
outdel.read((char *)&s,sizeof(MyFriend));
if(s.gettag()=='#')
temp.write((char *)&s,sizeof(MyFriend));
}
outdel.close();
temp.close();
fstream outdel1("MyFrd",ios::out|ios::trunc);
fstream temp1("temp",ios::in);
while(!temp1.eof())
{
temp1.read((char *)&s,sizeof(MyFriend));
outdel1.write((char *)&s,sizeof(MyFriend));
}
outdel1.close();
temp1.close();
cout<<"此记录已物理删除"<<endl;
}
int comp(char s1[],char s2[])
{
int i=0;
while(s1[i]!='\0' && s2[i]!='\0' && s1[i]==s2[i])
i++;
if(s1[i]=='\0' || s2[i]=='\0')
return 1;
else
return 0;
}
void main()
{
int sel;
do
{
cout<<"请选择(1:输入数据 2:输出数据 3:按姓名查询" <<endl
<<"4:添加数据 5:逻辑删除 6:物理删除 其它退出):";
cin>>sel;
switch(sel)
{
case 1:func1();break;
case 2:func2();break;
case 3:func3();break;
case 4:func4();break;
case 5:func5();break;
case 6:func6();break;
}
}while(sel>=1 && sel<=6);
}
//帮帮忙具体解释一下函数func6();