📄 filelibrary.cpp
字号:
int recseq;
int offset;
bool isdelete;
int datalenth;
char* rectag = new char[TagLenth];
char* tempdata = new char[Lenth_TempData];
cout<<"请输入记录标识(长度小于10,无空格符),以回车结束"<<endl;
cin>>rectag;
getchar(); //忽略输入缓冲的回车符
Record* temp;
//顺序读取链表直到当前指针前,判断文件标识表示是否重复
Record *current = &RecList.getValue();
for( RecList.setStart(); RecList.rightLenth()>0; RecList.next() ){
temp = &RecList.getValue();
if(strcmp(temp->getRecTag(),rectag) == 0){ //文件标识有重复,不允许
cout<<"记录标识\""<<rectag<<"\"与现有记录重复。写入记录不成功"<<endl;
//指针位置还原
if( current == NULL)
RecList.setEnd();
else{
while( RecList.rightLenth()>0){
temp = &RecList.getValue();
if(temp->getRecSeq() == current->getRecSeq())
break;
RecList.next();
}
}
return false;
}
if( current != NULL && temp->getRecSeq() == current->getRecSeq()) //已经去到当前指针位置,退出循环
break;
}
//得到当前插入记录的序列号
recseq = NextNum++;
offset = End;
isdelete = false;
/*
在链表中删除当前记录及以后的记录,在文件中打上删除标识
*/
Record t;
while(RecList.remove(t)){
File.seekg(t.getOffset()+Lenth_RecSeq+TagLenth, ios::beg);
File.put('1');
}
//写入记录
File.seekg(End,ios::beg); //定位要插入的位置
File.width(Lenth_RecSeq); //设置输出的宽度为MaxTagLenth
File.flags(ios::left); //设置为左对齐
File<<recseq;
File.fill('\0'); //设置为结束符,以便进行字符串比较
File.width(TagLenth);
File<<rectag;
File.fill(' '); //还原为空白符
File<<isdelete;
File.width(Lenth_Offset);
File<<End;
cout<<"记录内容为:"<<endl;
cout<<"1.字节流嵌入"<<endl;
cout<<"2.文件链接"<<endl;
cout<<"请输入选项的号码:"<<endl;
int choice;
cin>>choice;
getchar();
if(cin.fail()){
cout<<"输入错误!返回上一级菜单。"<<endl;
return false;
}
if(choice == 1){
cout<<"请输入记录内容,以ctrl+Z,回车,再Ctrl+z结束"<<endl;
cin.read(tempdata,Lenth_TempData);
File.width(Lenth_DataLenth);
datalenth = cin.gcount();
cin.clear(); //读入数据后,ios::eof被设置,还原状态
}
else if (choice == 2){
cout<<"请输入文件链接地址,按回车结束"<<endl;
char* link = new char[Lenth_TempData];
cin.get(link,Lenth_TempData);
File.width(Lenth_DataLenth);
datalenth = cin.gcount()+ 9; //数据内容的开始为"文件链接:",所以数据长度要加上9
getchar(); //跳过缓冲中的回车
tempdata = strcpy(tempdata,"filelink:");
tempdata = strcat(tempdata,link);
cin.clear(); //读入数据后,ios::eof被设置,还原状态
}
else
{
cout<<"输入错误!返回上一级菜单。"<<endl;
}
File<<datalenth;
File.write(tempdata,datalenth);
Record a(recseq,rectag,End,datalenth,false);
RecList.insert(a); //插入记录链表的尾部
RecList.next(); //当前记录指针后移
End = File.tellg(); //文件的结束位置后移
cout<<"写入新记录成功"<<endl;
File.clear();
return true;
}
bool FileFunction::AppendRec(){ //加到尾部
if(!CheckFileOpen()){
cout<<"文件尚未打开,无法添加记录"<<endl;
return false;
}
File.clear();
int recseq;
int offset;
bool isdelete;
int datalenth;
char* rectag = new char[TagLenth];
char* tempdata = new char[Lenth_TempData];
cout<<"请输入记录标识(长度小于10,无空格符),以回车结束"<<endl;
cin>>rectag;
getchar(); //忽略输入缓冲的回车符
Record* temp;
//顺序读取链表直到尾部,判断文件标识表示是否重复
// Record *current = &RecList.getValue();
for( RecList.setStart(); RecList.rightLenth()>0; RecList.next() ){
temp = &RecList.getValue();
if(strcmp(temp->getRecTag(),rectag) == 0){ //文件标识有重复,不允许
cout<<"记录标识\""<<rectag<<"\"与现有记录重复。添加记录不成功"<<endl;
return false;
}
}
//得到当前插入记录的序列号
recseq = NextNum++;
offset = End;
isdelete = false;
//写入记录
File.seekg(End,ios::beg); //定位要插入的位置
File.width(Lenth_RecSeq); //设置输出的宽度为MaxTagLenth
File.flags(ios::left); //设置为左对齐
File<<recseq;
File.fill('\0'); //设置为结束符,以便进行字符串比较
File.width(TagLenth);
File<<rectag;
File.fill(' '); //还原为空白符
File<<isdelete;
File.width(Lenth_Offset);
File<<End;
cout<<"记录内容为:"<<endl;
cout<<"1.字节流嵌入"<<endl;
cout<<"2.文件链接"<<endl;
cout<<"请输入选项的号码:"<<endl;
int choice;
cin>>choice;
getchar();
if(cin.fail()){
cout<<"输入错误!返回上一级菜单。"<<endl;
return false;
}
if(choice == 1){
cout<<"请输入记录内容,以ctrl+Z,回车,再Ctrl+z结束"<<endl;
cin.read(tempdata,Lenth_TempData);
File.width(Lenth_DataLenth);
datalenth = cin.gcount();
cin.clear(); //读入数据后,ios::eof被设置,还原状态
}
else if (choice == 2){
cout<<"请输入文件链接地址,按回车结束"<<endl;
char* link = new char[Lenth_TempData];
cin.get(link,Lenth_TempData);
File.width(Lenth_DataLenth);
datalenth = cin.gcount()+ 9; //数据内容的开始为"文件链接:",所以数据长度要加上9
getchar(); //跳过缓冲中的回车
tempdata = strcpy(tempdata,"filelink:");
tempdata = strcat(tempdata,link);
cin.clear(); //读入数据后,ios::eof被设置,还原状态
}
else
{
cout<<"输入错误!返回上一级菜单。"<<endl;
}
File<<datalenth;
File.write(tempdata,datalenth);
Record a(recseq,rectag,End,datalenth,false);
RecList.append(a);
End = File.tellg(); //文件的结束位置后移
cout<<"写入新记录成功"<<endl;
File.clear();
return true;
}
bool FileFunction::InsertCurrentRec(){ //加到当前记录之前
if(!CheckFileOpen()){
cout<<"文件尚未打开,无法写入"<<endl;
return false;
}
File.clear();
int recseq;
int offset;
bool isdelete;
int datalenth;
char* rectag = new char[TagLenth];
char* tempdata = new char[Lenth_TempData];
cout<<"请输入记录标识(长度小于10,无空格符),以回车结束"<<endl;
cin>>rectag;
getchar(); //忽略输入缓冲的回车符
Record* temp;
//顺序读取链表到尾部,判断文件标识表示是否重复
Record *current = &RecList.getValue();
for( RecList.setStart(); RecList.rightLenth()>0; RecList.next()){
temp = &RecList.getValue();
if( strcmp( temp->getRecTag(), rectag) == 0){
cout<<"记录标识\""<<rectag<<"\"与现有记录重复。写入记录不成功"<<endl;
return false;
}
}
//还原指针
for( RecList.setStart(); RecList.rightLenth()>0; RecList.next()){
temp = &RecList.getValue();
if( current->getRecSeq() == temp->getRecSeq() )
break;
}
//得到当前插入记录的序列号
recseq = NextNum++;
offset = End;
isdelete = false;
//写入记录
File.seekg(End,ios::beg); //定位要插入的位置
File.width(Lenth_RecSeq); //设置输出的宽度为MaxTagLenth
File.flags(ios::left); //设置为左对齐
File<<recseq;
File.fill('\0'); //设置为结束符,以便进行字符串比较
File.width(TagLenth);
File<<rectag;
File.fill(' '); //还原为空白符
File<<isdelete;
File.width(Lenth_Offset);
File<<End;
cout<<"记录内容为:"<<endl;
cout<<"1.字节流嵌入"<<endl;
cout<<"2.文件链接"<<endl;
cout<<"请输入选项的号码:"<<endl;
int choice;
cin>>choice;
getchar();
if(cin.fail()){
cout<<"输入错误!返回上一级菜单。"<<endl;
return false;
}
if(choice == 1){
cout<<"请输入记录内容,以ctrl+Z,回车,再Ctrl+z结束"<<endl;
cin.read(tempdata,Lenth_TempData);
File.width(Lenth_DataLenth);
datalenth = cin.gcount();
cin.clear(); //读入数据后,ios::eof被设置,还原状态
}
else if (choice == 2){
cout<<"请输入文件链接地址,按回车结束"<<endl;
char* link = new char[Lenth_TempData];
cin.get(link,Lenth_TempData);
File.width(Lenth_DataLenth);
datalenth = cin.gcount()+ 9; //数据内容的开始为"文件链接:",所以数据长度要加上9
getchar(); //跳过缓冲中的回车
tempdata = strcpy(tempdata,"filelink:");
tempdata = strcat(tempdata,link);
cin.clear(); //读入数据后,ios::eof被设置,还原状态
}
else
{
cout<<"输入错误!返回上一级菜单。"<<endl;
}
File<<datalenth;
File.write(tempdata,datalenth);
Record a(recseq,rectag,End,datalenth,false);
RecList.insert(a); //插入记录链表的当前位置
End = File.tellg(); //文件的结束位置后移
cout<<"插入新记录成功"<<endl;
File.clear();
return true;
}
bool FileFunction::DeleteCurrentRec(){ //删除当前记录
if(!CheckFileOpen()){
cout<<"文件尚未打开,无法删除记录"<<endl;
return false;
}
Record t;
if(RecList.remove(t)){
File.seekg(t.getOffset()+Lenth_RecSeq+TagLenth, ios::beg);
File.put('1');
cout<<"删除记录成功"<<endl;
File.clear();
return true;
}
else{
cout<<"删除记录不成功"<<endl;
File.clear();
return false;
}
}
bool FileFunction::CheckIsBegin(){ //判别当前记录指针是否已到达文件头,注意是逻辑顺序上的头,而不是物理顺序上的头
//文件指针是否去到链表头
return RecList.leftLenth()==0;
}
bool FileFunction::CheckIsEnd(){//判别当前记录指针是否已到达文件尾(结束标记)
//文件指针已经去到尾部
return RecList.rightLenth() == 0;
}
bool FileFunction::CheckFileOpen(){
return File.is_open();
}
bool FileFunction::ForwardPointer(){//顺序移动记录指针(向前)
if(RecList.rightLenth() == 0)
return false;
RecList.next();
return true;
}
bool FileFunction::BackwardPointer(){//顺序移动记录指针(向后)
if(RecList.leftLenth() == 0)
return false;
RecList.prev();
return true;
}
bool FileFunction::LocateByTag(char* tag){ //按记录标识定位记录;
Record* temp = NULL;
for(RecList.setStart(); RecList.rightLenth() > 0; RecList.next()){
temp = &RecList.getValue();
if( strcmp(tag, temp->getRecTag() ) == 0)
return true;
}
return false;
}
bool FileFunction::LocateBySeq(int seq){//按记录号定位记录
if (seq <= 0)
return false;
Record *temp;
for( RecList.setStart(); RecList.rightLenth()>0 ; RecList.next()){
temp = &RecList.getValue();
if(temp->getRecSeq() == seq)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -