}
//从物理文件名为fn1指针所指主文件中删除n个记录
template
void InFile
{//定义一个输入输出文件流对象fio,与主文件相联系
fstream fio(fn1,ios::in|ios::out|ios::binary);
if(!fio)
{cerr<
T x;
T1 y;
int i;
//依次删除每条记录
for(i=0; i
bool k=IFDelete(fn2,x);
if(!k)
{cout<<"关键字为"<
fio.read((char*) &y, b1);
y.key=DMark;
fio.seekg(-b1, ios::cur);
fio.write((char*) &y, b1);
cout<<"关键字为"<
}
//从物理文件名为fn1指针所指主文件中查找n个记录
template
void InFile
{//定义一个输入输出文件流对象ifs,与主文件相联系
ifstream ifs(fn1,ios::in|ios::binary);
if(!ifs)
{cerr<
T x;
T1 y;
int i;
//依次查找每条记录
for(i=0; i
bool k=IFSearch(fn2,x);
if(!k)
{cout<<"查找关键字为"<
ifs.read((char*) &y, b1);
cout<<"查找关键字为"<
}
//把一个记录的索引项插入到有序数组A中
template
void InFile
{int i;
for(i=mm-1;i>=0;i--)
{//从尾部向前为寻找插入位置进行顺序比较和移动
if(A[i].key>x.key) A[i+1]=A[i];
else {A[i+1]=x; break;}}
if(i<0) A[0]=x;
}
//向由fn2指针所表示的索引有序文件中插入x索引项
template
void InFile
{//以输入方式打开由fn2指针所表示的索引有序文件
fstream ifs(fn2,ios::in|ios::binary);
if(!ifs)
{cerr<
ofstream ofs(".\\temp",ios::out|ios::binary);
if(!ofs)
{cerr<<"temp"<<' '<<"not open!"<
T* A=new T[m+1];
//将原文件指针指向文件开始位置,此语句可省略
ifs.seekg(0);
//通过while循环完成插入操作
int b2=sizeof(T);
while(1)
int s=ifs.gcount()/b2;
//读入数组A的实际索引项数被存入s中
if(s==m)
{if(A[m-1].key
else {
SeqInsert(A, m, x);//将x索引项插入到有序数组A中
ofs.write((char*)A,(m+1)*b2);
while(!ifs.eof())
{ //把原文件中剩余的所有记录写入到结果文件中
ifs.read((char*)A, m*b2);
s=ifs.gcount()/b2;
ofs.write((char*)A, s*b2);}
break; //退出while(1)循环
}}
else
{SeqInsert(A, s, x);
ofs.write((char*)A, (s+1)*b2);
break;//处理完最后一个数据块时退出while(1)循环
}}
delete [] A;
ifs.close();
ofs.close();
remove(fn2);
rename("temp",fn2);
}
//从有序数组A中删除一个关键字为x.key的索引项
template
bool InFile
{//从数组A的首元素开始顺序查找关键字为x.key的索引项
int i=0;
while(i
if(i==mm || A[i].key!=x.key) return false;
//被删除的索引项赋给x带回
x=A[i];
//使i+1至mm-1的所有元素前移一个位置
for(int j=i+1; j
return true; www.2cto.com
}
//从由fn2指针所表示的索引有序文件中删除
//关键字为x.key的索引项,并由x带回被删除的索引项
template
bool InFile
{//以输入方式打开由fn2指针所表示的索引有序文件
ifstream ifs(fn2, ios::in|ios::binary);
if(!ifs) {
cerr<
ofstream ofs("temp",ios::out|ios::binary);
if(!ofs) {
cerr<<"temp"<<' '<<"not open!"<
//动态定义一个具有m个元素的数组A
T* A=new T[m];
//用d等于true或false表示删除是否成功
bool d;
//通过while完成删除操作
while(1)
{ifs.read((char*)A, m*b2);
int s=ifs.gcount()/b2;
//读入数组A的实际索引项数被存入s中
if(s==m)
{if(A[m-1].key
else {d=SeqDelete(A, m, x);
if(d) ofs.write((char*)A,(m-1)*b2);
else ofs.write((char*)A,m*b2);
while(!ifs.eof())
{ //把原文件中剩余的所有记录写入到结果文件中
ifs.read((char*)A, m*b2);
s=ifs.gcount()/b2;
ofs.write((char*)A, s*b2);}
break; //退出while(1)循环
}}
else