📄 filelibrary.cpp
字号:
cout<<"***************************************"<<endl;
cout<<"* *"<<endl;
cout<<"* 1.按记录号删除指定的记录 *"<<endl;
cout<<"* 2.按记录标识删除指定的记录 *"<<endl;
cout<<"* 3.删除当前记录 *"<<endl;
cout<<"* 0.返回主菜单 *"<<endl;
cout<<"* *"<<endl;
cout<<"***************************************"<<endl;
cout<<"请输入选项的号码:";
int choice;
cin>>choice;
if(cin.fail()){
cout<<"输入错误!返回主菜单。"<<endl;
return;
}
switch(choice){
case 1:{
int seq;
cout<<"请输入记录的序号:";
cin>>seq;
if(LocateBySeq(seq)){
DeleteCurrentRec();
}
else{
cout<<"不存在序号为"<<seq<<"的记录"<<endl;
}
break;
}
case 2:{
char* tag = new char[TagLenth+1];
cout<<"请输入记录的标识:";
cin>>tag;
if(LocateByTag(tag)){
DeleteCurrentRec();
}
else{
cout<<"不存在以"<<tag<<"为标识的记录。"<<endl;
}
break;
}
case 3:{
InsertCurrentRec();
break;
}
case 0: return; //返回主菜单
default: cout << "输入错误,请再次输入"<<endl; break;
}
cout<<endl;
}
}
void FileFunction::MenuCheckPointer(){//判断当前记录指针位置,即分别判别当前记录指针是否已到达文件尾(结束标记)和头;
if(!CheckFileOpen())
cout<<"文件尚未打开"<<endl;
else if(CheckIsBegin())
cout<<"记录指针在文件头"<<endl;
else if(CheckIsEnd())
cout<<"记录指针在文件尾"<<endl;
else
cout<<"记录指针在文件中间"<<endl;
cout<<"请按任意键加回车,返回主菜单"<<endl;
char a;
cin>>a;
}
void FileFunction::MenuCount(){
system("cls");
while(1){
cout<<"****************************"<<endl;
cout<<"* *"<<endl;
cout<<"* 计 数 *"<<endl;
cout<<"* *"<<endl;
cout<<"****************************"<<endl;
cout<<"* 1.文件记录数 *"<<endl;
cout<<"* 2.文件的总字节数 *"<<endl;
cout<<"* 0.返回主菜单 *"<<endl;
cout<<"* *"<<endl;
cout<<"****************************"<<endl;
cout<<"请输入选项的号码:";
int choice;
cin>>choice;
if(cin.fail()){
cout<<"输入错误!返回主菜单。"<<endl;
return;
}
switch(choice){
case 1: cout<<endl<<"文件共有"<<CountRec()<<"条记录"<<endl;
break;
case 2: {
long cnt = CountByte();
if( cnt != -1)
cout<<"文件的总字节数是"<<cnt<<"。"<<endl;
else
cout<<"文件尚未被打开。"<<endl;
break;
}
case 0: return; //返回主菜单
default: cout << "输入错误,请再次输入"<<endl; break;
}
cout<<endl;
}
}
bool FileFunction::MenuReadIndex(){
if(!CheckFileOpen())
return false; //没有文件被打开
ofstream indexfile;
char* index = new char[20];
index = strcpy(index,FileName);
index = strcat(index,"index.txt"); //索引文件为 FileNameindex.txt
indexfile.open(index);
/*先把内存里的链表内容输出进索引文件中*/
indexfile.width(Lenth_RecSeq); //设置输出的宽度为MaxTagLenth
indexfile.flags(ios::left); //设置为左对齐
indexfile<<"记录号";
indexfile.width(TagLenth);
indexfile<<"记录标识";
indexfile.width(Lenth_IsDelete);
indexfile<<"删除标志";
indexfile.width(Lenth_Offset);
indexfile<<"偏移位置";
indexfile.width(Lenth_DataLenth);
indexfile<<"数据长度"<<endl;
Record* temp;
for(RecList.setStart(); RecList.rightLenth()>0; RecList.next()){
temp = &RecList.getValue();
indexfile.width(Lenth_RecSeq); //设置输出的宽度为MaxTagLenth
indexfile.flags(ios::left); //设置为左对齐
indexfile<<temp->getRecSeq();
indexfile.fill('\0'); //设置为结束符,以便进行字符串比较
indexfile.width(TagLenth);
indexfile<<temp->getRecTag();;
indexfile.fill(' '); //还原为空白符
indexfile.width(Lenth_IsDelete);
indexfile<<temp->CheckIsDelete();
indexfile.width(Lenth_Offset);
indexfile<<temp->getOffset();
indexfile.width(Lenth_DataLenth);
indexfile<<temp->getDataLenth()<<endl;
}
indexfile.close();
system(index); //直接链接打开
return true;
}
void FileFunction::MenuWriteToFile(){
system("cls");
while(1){
cout<<"******************************"<<endl;
cout<<"* *"<<endl;
cout<<"* 写 为 文 件 *"<<endl;
cout<<"* *"<<endl;
cout<<"******************************"<<endl;
cout<<"* 1.把若干记录写进新文件 *"<<endl;
cout<<"* 2.把所有记录写进新文件 *"<<endl;
cout<<"* 0.返回主菜单 *"<<endl;
cout<<"* *"<<endl;
cout<<"******************************"<<endl;
cout<<"请输入选项的号码:";
int choice;
cin>>choice;
if(cin.fail()){
cout<<"输入错误!返回主菜单。"<<endl;
return;
}
switch(choice){
case 1: {
WriteSomeToFile();
break;
}
case 2: {
WriteAllToFile();
break;
}
case 0: return; //返回主菜单
default: cout << "输入错误,请再次输入"<<endl; break;
}
cout<<endl;
}
}
bool FileFunction::Create(char* filename){
//若已经打开文件,先复原。
if(CheckFileOpen())
Close();
File.open(filename, fstream::in | fstream::out | fstream::trunc | fstream::binary ); //创建一个空文件,并处于读写状态。
if(!File)
return false;
FileName = filename;
End = 0;
NextNum = 1;
return true;
}
bool FileFunction::Open(char* filename){
//若已经打开文件,先复原。
if(CheckFileOpen())
Close();
File.open(filename,ios::in | ios::out | ios::binary ); //打开已有的文件,并处于读写状态。
if(!File){
File.clear();
return false;
}
FileName = filename;
End = 0;
/*同步打开索引文件*/
char* index = new char[20];
index = strcpy(index,FileName);
index = strcat(index,"index.txt"); //索引文件为 FileNameindex.txt
ifstream indexfile;
indexfile.open(index);
if(!indexfile){ //索引文件没有打开成功
cout<<"索引文件没有打开成功"<<endl;
File.close();
return false;
}
indexfile.seekg(52,ios::beg); //跳过文件开头的标题行
//初始化链表
while(1)
{
//分配空间
char* recseq = new char[Lenth_RecSeq+1];
char* rectag = new char[TagLenth+1];
bool isdelete;
char* offset = new char[Lenth_Offset+1];
char* datalenth = new char[Lenth_DataLenth+1];
//读取文件;
indexfile.get(recseq,Lenth_RecSeq+1);
if(atoi(recseq)==0) //若序列号为0,此记录有错,退出。
break;
if(indexfile.fail()){ //已经读取完文件
File.clear();
break;
}
//读标识
indexfile.get(rectag,TagLenth+1);
//读取删除标识
char ch;
indexfile.get(ch);
isdelete = ch-'0';
indexfile.ignore(Lenth_IsDelete-1);
//读取偏移位置
indexfile.get(offset,Lenth_Offset+1);
//读取数据长度
indexfile.get(datalenth,Lenth_DataLenth+1);
int lenth = atoi(datalenth);
indexfile.get(ch); //忽略索引文件中的回车符
if( !isdelete){ //记录未被删除,插入进记录链表中
Record temp(atoi(recseq),rectag,atoi(offset),lenth,isdelete);
/*
cout<<"当前记录:"<<endl;
cout<<"序号:"<<temp.getRecSeq()<<endl;
cout<<"标识:"<<temp.getRecTag()<<endl;
cout<<"删除标志:"<<temp.CheckIsDelete()<<endl;
cout<<"偏移位置:"<<temp.getOffset()<<endl;
cout<<"数据长度:"<<temp.getDataLenth()<<endl;
*/
RecList.append(temp);
}
NextNum = ( NextNum > atoi(recseq)+1 ? NextNum :atoi(recseq)+1 );
}
indexfile.close();
File.seekg(0,ios::end);
End = File.tellg(); //偏移位置去到文件尾
return true;
}
bool FileFunction::Close(){ //关闭一个已打开的文件,缓冲区全部内容写回外存;
if(!CheckFileOpen())
return false; //没有文件被打开,关闭不成功
ofstream indexfile;
char* index = new char[20];
index = strcpy(index,FileName);
index = strcat(index,"index.txt"); //索引文件为 FileNameindex.txt
indexfile.open(index);
indexfile.width(Lenth_RecSeq); //设置输出的宽度为MaxTagLenth
indexfile.flags(ios::left); //设置为左对齐
indexfile<<"记录号";
indexfile.width(TagLenth);
indexfile<<"记录标识";
indexfile.width(Lenth_IsDelete);
indexfile<<"删除标志";
indexfile.width(Lenth_Offset);
indexfile<<"偏移位置";
indexfile.width(Lenth_DataLenth);
indexfile<<"数据长度"<<endl;
Record* temp;
for(RecList.setStart(); RecList.rightLenth()>0; RecList.next()){
temp = &RecList.getValue();
indexfile.width(Lenth_RecSeq); //设置输出的宽度为MaxTagLenth
indexfile.flags(ios::left); //设置为左对齐
indexfile<<temp->getRecSeq();
indexfile.fill('\0'); //设置为结束符,以便进行字符串比较
indexfile.width(TagLenth);
indexfile<<temp->getRecTag();;
indexfile.fill(' '); //还原为空白符
indexfile.width(Lenth_IsDelete);
indexfile<<temp->CheckIsDelete();
indexfile.width(Lenth_Offset);
indexfile<<temp->getOffset();
indexfile.width(Lenth_DataLenth);
indexfile<<temp->getDataLenth()<<endl;
}
indexfile.close();
RecList.clear(); //清空链表
File.close();
File.clear();
return true;
}
bool FileFunction::ReadByOrder(){
//文件记录为空的情况
if(RecList.leftLenth()+RecList.rightLenth() == 0)
return false;
if( CheckIsEnd())
return false; //记录指针已经到达文件尾
Record *temp = &RecList.getValue();
cout<<"当前记录的序号是"<< temp->getRecSeq()<<endl;
cout<<"标识:"<< temp->getRecTag()<<endl;
// cout<<"删除标志:"<< temp->CheckIsDelete()<<endl;
// cout<<"偏移位置:"<< temp->getOffset()<<endl;
// cout<<"数据长度:"<< temp->getDataLenth()<<endl;
File.seekg( temp->getOffset()+Lenth_RecSeq+TagLenth +1 +Lenth_Offset + Lenth_DataLenth,ios::beg);
char* data = new char[ temp->getDataLenth()+1];
File.read(data, temp->getDataLenth()); //存的时候是以write来存,所遇取的时候对应用read
char* s = new char[10];
s = strncpy( s, data,9);
s[9]='\0';
if(strcmp(s,"filelink:")==0){ //数据内容为文件链接
int t = temp->getDataLenth()-9;
char* newfilename = new char[t+1];
for(int i=0; i < t; i++ ){
newfilename[i] = data[9+i];
}
newfilename[t]='\0';
cout<<"此记录为文件"<<newfilename<<"的链接"<<endl;
system(newfilename);
}
else{
data[ temp->getDataLenth() ]='\0'; //设置最后一个字符为结束符号
cout<<"数据内容:"<<endl<<data<<endl;
}
//当前记录指针后移
FileFunction::ForwardPointer();
return true;
}
bool FileFunction::WriteByOrder(){
if(!CheckFileOpen()){
cout<<"文件尚未打开,无法写入"<<endl;
return false;
}
File.clear();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -