⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filefunction.h

📁 数据结构大作业——变长记录文件存取类库 设计一个类库
💻 H
📖 第 1 页 / 共 4 页
字号:
				 cout << "请输入记录标志:\n";
				 cin >> temp.recordtag;
				 fstream f;
				 f.open( temp.fname , ios::binary | ios::in);
				 if( f.fail() )
				 {
					 f.close();
					 ofstream ouf( temp.fname, ios::binary | ios::out );
					 cin.clear();
					 cout << "请输入内容: \n";
					 cin.get();
					 ouf << cin.rdbuf();
					 ouf.seekp(0,ios::end);
					 long pos=ouf.tellp();
					 ouf.seekp(0,ios::beg);
					 pos-= ouf.tellp();
					 temp.blocklength = pos;
					 ouf.close();

					 Listlink.insert( apptemp, temp );     //写指针指向新结点
					 apptemp--;
					 currentwrite=apptemp;					//移动写指针
					 apptemp++;
					 while(apptemp!=Listlink.end())
					 {
						 (*apptemp).data_num++;
						 apptemp++;
					}
					 	
					 temp.data_num = 0;
				
					 temp.fname = new char[255];
					 temp.recordtag = new char[500];
					 strcpy( temp.recordtag, "no record");
 					 temp.deletetag = false;
    	 	     	temp.blocklength = 0;
					 break;
				 }
				 else
				 {
					     cout << "文件已存在,现与之链接……" << endl;
						 f.open(filename, ios::binary);
						 f.seekp(0,ios::end);
						 long pos=f.tellp();
						 f.seekp(0,ios::beg);
 						 pos-= f.tellp();
 						 temp.blocklength = pos;
 						 f.close();

						 Listlink.insert( apptemp, temp );
						 apptemp--;
						currentwrite=apptemp;					//移动写指针
						apptemp++;
						while(apptemp!=Listlink.end())
						{
							 (*apptemp).data_num++;
							apptemp++;
						}
						
						temp.data_num = 0;
						
						temp.fname = new char[255];
						temp.recordtag = new char[500];
						strcpy( temp.recordtag, "no record");
						temp.deletetag = false;
						temp.blocklength = 0;
						break;
				 }
			 }
			 else
			 {
				 	 cout << "\n该文件名已在记录中存在\n";
					 break;		 
			 }
		 }
		}
		 
	 case 4:							//插到指定记录标志之前
		 {
			 char *rec=new char[500];
			 cout << "\n请输入记录标志:\n";
			 cin >> rec;
	    	  currenttemp=Listlink.begin();
			do
			{
			 recordlocate(rec);
			 if(currenttemp==Listlink.end())
			 {
				 break;
			 }
			 
			 list<record>::iterator rectemp=currenttemp;

			  char *filename = new char[255];
			 cout << "\n请输入文件路径:\n";
			 cin >> filename;
			
			
			 if(!filename)
			{
				cerr << "文件无法打开" << endl;
				abort();
			}
		   
			 else
			{
			  cout << endl;
			  filelocate( filename );
			  if( currenttemp == Listlink.end() )
			 {
				 dnum++;
				 temp.data_num = (*rectemp).data_num;
			     strcpy( temp.fname, filename);
	
				 cout << "请输入记录标志:\n";
				 cin >> temp.recordtag;
				 
				fstream f;
				f.open( temp.fname ,  ios::binary | ios::in );
				 if( f.fail() )
				 {
					 f.close();
					 ofstream ouf( temp.fname, ios::out | ios::binary);
					 cin.clear();
					 cout << "请输入内容: \n";
					 cin.get();
					 ouf << cin.rdbuf();
					 ouf.seekp(0,ios::end);
					 long pos=ouf.tellp();
					 ouf.seekp(0,ios::beg);
					 pos-= ouf.tellp();
					 temp.blocklength = pos;
					 ouf.close();

					 Listlink.insert( rectemp, temp );     //写指针指向新结点
					 currenttemp=rectemp;
					 rectemp--;
					 currentwrite=rectemp;					//移动写指针
					 rectemp++;
					 while(rectemp!=Listlink.end())
					 {
						 (*rectemp).data_num++;
						 rectemp++;
					}
					 	
					 temp.data_num = 0;
					 
					 temp.fname = new char[255];
					 temp.recordtag = new char[500];
					 strcpy( temp.recordtag, "no record");
 					 temp.deletetag = false;
    	 	     	temp.blocklength = 0;
					 break;
				 }
				 else
				 {
					     cout << "文件已存在,现与之链接……" << endl;
						 f.open(filename, ios::binary);
						 f.seekp(0,ios::end);
						 long pos=f.tellp();
						 f.seekp(0,ios::beg);
 						 pos-= f.tellp();
 						 temp.blocklength = pos;
 						 f.close();

						 Listlink.insert( rectemp, temp );
						 currenttemp=rectemp;
						 rectemp--;
						currentwrite=rectemp;					//移动写指针
						rectemp++;
						while(rectemp!=Listlink.end())
						{
							 (*rectemp).data_num++;
							rectemp++;
						}
						
						temp.data_num = 0;
					
						temp.fname = new char[255];
						temp.recordtag = new char[500];
						strcpy( temp.recordtag, "no record");
						temp.deletetag = false;
						temp.blocklength = 0;
						break;
				 }
			 }
			 else
			 {
				 	 cout << "\n该文件名已在记录中存在\n";
					 break;		 
			 }
			}
			}
			while( currenttemp!=Listlink.end() );
		}
	}
 }while(w!=0);
}



void function::Rewrite()
//重写记录
{
    int R;
	do
	{
		R=0;
		cout << "\n重写指定记录\n";
		cout << "1------按指定记录号重写记录\n"
			 << "2------重写当前写指针记录\n"
			 << "3------重写当前读指针记录\n"
			 << "4------按指定记录标志重写记录\n"
			 << "0------返回主目录\n";
		cin >> R;
		switch(R)
		{
		case 0: break;
		case 1:
			{
				cout <<"\n请输入记录号\n";
				int s;
				cin >> s;
				locate(s);
				if( temp.data_num==-1) 
				{ 
					cout << "输入错误\n";
					temp.data_num=0;
					break; 
				}
				
				if( (*currenttemp).deletetag==true )
				{
					cout << "该记录已删除,请重新输入文件名";
					int k=0;
					do
					{
						k=0;
						cin >> (*currenttemp).fname;
						fstream f( (*currenttemp).fname, ios::in | ios::binary);
						if( !f.fail() )
						{
							cout << "文件名已存在,请重新输入\n";
							k=1;
						}
						f.close();
					}
					while(k==1);
				}

				cout << "\n请输入记录标志:\n";
				cin >>(*currenttemp).recordtag;
		
				ofstream ouf;
				ouf.open( (*currenttemp).fname , ios::out | ios::binary);
				cin.clear();
				cout << "请输入内容: \n";
				cin.get();
				ouf << cin.rdbuf();
				
				ouf.seekp(0,ios::end);
				long pos=ouf.tellp();
				ouf.seekp(0,ios::beg);
				pos-= ouf.tellp();
				(*currenttemp).blocklength = pos;
				cout << "修改成功!\n";
				ouf.close();
				
				(*currenttemp).deletetag=false;
				break;
			}

		case 2:
			{
					if( (*currentwrite).deletetag==true )
				{
					cout << "该记录已删除,请重新输入文件名";
					int k;
					do
					{
						k=0;
						cin >> (*currentwrite).fname;
						fstream f( (*currentwrite).fname, ios::in | ios::binary);
						if( !f.fail() )
						{
							cout << "文件名已存在,请重新输入\n";k=1;
						}
						f.close();
					}
					while(k==1);
				}

				cout << "\n请输入记录标志:\n";
				cin >>(*currentwrite).recordtag;
		
				ofstream ouf;
				ouf.open( (*currentwrite).fname , ios::out | ios::binary);
				cin.clear();
				cout << "请输入内容: \n";
				cin.get();
				ouf << cin.rdbuf();
				
				ouf.seekp(0,ios::end);
				long pos=ouf.tellp();
				ouf.seekp(0,ios::beg);
				pos-= ouf.tellp();
				(*currentwrite).blocklength = pos;
				cout << "修改成功!\n";
				ouf.close();
				
				(*currentwrite).deletetag=false;
				break;
			}

		case 3:
			{
					if( (*currentread).deletetag==true )
				{
					cout << "该记录已删除,请重新输入文件名";
					int k;
					do
					{
						k=0;
						cin >> (*currentread).fname;
						fstream f( (*currentread).fname, ios::in | ios::binary);
						if( !f.fail() )
						{
							cout << "文件名已存在,请重新输入\n";
							k=1;
						}
						f.close();
					}
					while(k==1);
				}

				cout << "\n请输入记录标志:\n";
				cin >>(*currentread).recordtag;
		
				ofstream ouf;
				ouf.open( (*currentread).fname , ios::out | ios::binary);
				cin.clear();
				cout << "请输入内容: \n";
				cin.get();
				ouf << cin.rdbuf();
				
				ouf.seekp(0,ios::end);
				long pos=ouf.tellp();
				ouf.seekp(0,ios::beg);
				pos-= ouf.tellp();
				(*currentread).blocklength = pos;
				cout << "修改成功!\n";
				ouf.close();
				
				(*currentread).deletetag=false;
				break;
			}

		case 4:
			{
			 char *rec=new char[500];
			 cout << "\n请输入记录标志:\n";
			 cin >> rec;
				int a=0;
				int R[500];
				list<record>::iterator findtemp = Listlink.begin();	
				for( int i=1; i<=dnum || findtemp!=Listlink.end() ; findtemp++,i++)
				{
					string Data1="";
					Data1+=(*findtemp).recordtag;
					if( (Data1.find(rec))!=-1)
					{
						 cout << "与你输入条件匹配的有第" << i << "个记录" << endl;
						 R[a]=i;
						 a++;
					}
				}
				
					if(a==0)
					{
						cout << "不存在与您输入条件匹配的记录\n";
					}
					if(a==1)
					{
						locate(R[0]);
						if( temp.data_num==-1) 
						{ 
						cout << "输入错误\n";
						temp.data_num=0;
						break; 
						}
				
						if( (*currenttemp).deletetag==true )
						{
							cout << "该记录已删除,请重新输入文件名";
							int k;
							do
							{
								k=0;
								cin >> (*currenttemp).fname;
								fstream f( (*currenttemp).fname, ios::in | ios::binary);
								if( !f.fail() )
								{
									cout << "文件名已存在,请重新输入\n";
									k=1;
								}
									f.close();
							}
							while(k==1);
						}
	
						cout << "\n请输入记录标志:\n";
						cin >>(*currenttemp).recordtag;
				
						ofstream ouf;
						ouf.open( (*currenttemp).fname , ios::out | ios::binary);
						cin.clear();
						cout << "请输入内容: \n";
						cin.get();
						ouf << cin.rdbuf();
						
						ouf.seekp(0,ios::end);
						long pos=ouf.tellp();
						ouf.seekp(0,ios::beg);
						pos-= ouf.tellp();
						(*currenttemp).blocklength = pos;
						cout << "修改成功!\n";
						ouf.close();
					
						(*currenttemp).deletetag=false;
						break;
					}
					if(a>1)
					{
						cout << "请通过选择输入记录号选择\n";
						break;
					}
			}
		default:
			{
				cout << "输入错误\n" ;
				break;
			}
		}
	}
	while(R!=0);
}
						





int function::getread() const
{
  cout << "读指针位于第" <<  (*currentread).data_num << endl;
  if(  (*currentread).data_num != dnum )
  {
		if( (*currentread).data_num==1 )
		{
			cout << "读指针位于文件头" << endl;
		}
  }
  else
  {
	   cout << "读指针位于文件尾" << endl;
  }
  return (*currentread).data_num;
}



int function::getwrite() const
{
  cout << "写指针位于第" <<  (*currentwrite).data_num << endl;
  if( (*currentread).data_num==dnum )
  {
	  cout << "写指针位于文件尾" << endl;
  }
  return (*currentwrite).data_num;
}


void function::index()
{
	ofstream Index( "c:\\index.txt", ios::out );               
	// 建立索引文件在"c:\index.txt"
	Index << "建立索引表\n";
	Index << "记录号\t" << "文件名\t\t" << "文件长度\t" << "删除标志\t" << "记录标志\n";
	//写入
	list<record>::iterator itr=Listlink.begin();
	while( itr!= Listlink.end() )
	{
        int q=0;
		if( (*itr).deletetag==true ){ q=1; }      
		Index << (*itr).data_num << '\t'           //写入记录
			  << (*itr).fname << '\t' << '\t'
			  << (*itr).blocklength << '\t'
			  << q << '\t' << '\t'
			  << (*itr).recordtag << '\n';
	    itr++;
	}
}

#endif


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -