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

📄 filefunction.h

📁 数据结构大作业——变长记录文件存取类库 设计一个类库
💻 H
📖 第 1 页 / 共 4 页
字号:

			fin.getline( temp1.fname, 255);  //读出记录文件名
			int u=0;
			while( temp1.fname[u]!='\r'){ u++;}  
			temp1.fname[u]='\0';
			//因为写入时最后一个字符为回车,故要将其改为\0

			fin.getline( buffer2, 10);      //读出记录长度
			temp1.blocklength = atoi( buffer2 );

			fin.getline( buffer3,10);       //读出删除标志
			if( atoi( buffer3 )==1 ) 
			{
				temp1.deletetag= true;
			}
			else 
			{ 
				temp1.deletetag = false ;
			}
				         			
		    fin.getline( temp1.recordtag, 500);  //读出记录标识
			int v=0;
			while( temp1.recordtag[v]!='\r'){ v++;}
			temp1.recordtag[v]='\0';
						
			Listlink.push_back( temp1 );    //加到链尾

			delete []buffer1;
			delete []buffer2;
			delete []buffer3;
			fin.close();
			i++;
		}

		else
		{
    		break;
		}
		delete []file;
		delete []buffer;
	}

    ifstream F;
	F.open("c:\\current.txt", ios::in );   
	//读写指针保存在c:\current.txt
	if( !F.fail() )
	{
		char *buffer4 = new char[20];
		char *buffer5 = new char[20];
		F.getline( buffer4,10);	  //读出读指针
		F.getline( buffer5,10);   //读出写指针
		read = atoi( buffer4 );
		write = atoi( buffer5 );
		
		dnum = write;
		for(int j=1; j<=read; j++)
		{
			currentread++;       //移动读指针到其位置
		}
		for( int k=1; k<=write; k++)
		{
			currentwrite++;     //移动写指针到其位置
		}
		
		delete []buffer4;
		delete []buffer5;
	}

	else
	{
		delete []buffer4;
		delete []buffer5;
	}

}		

  
void function::fileclose()
//关闭文件函数,关闭一个已打开的文件,并将缓冲区全部内容写回外存
{
  char *file1=new char[10];
  strcpy(file1, ".txt");                           // file1=".txt"
  
  list<record>::iterator  temp1;
  list<record>::iterator  temp2;
  
  int j=0;
                     
  for( temp1= Listlink.begin(); temp1 != Listlink.end(); j++,temp1++ )
  {	
	  char *file = new char[30];
	  char *buffer1 = new char[20];
	  char *buffer2 = new char[20];
	  char *buffer3 = new char[20];
      char *buffer4 = new char[20];
      
	 int k=j+1;
	  strcpy(file, "c:\\data");		//file="c:\\data"
	_itoa( k, buffer1, 10 );		//Convert an integer to a string
	strcat( file, buffer1);
	strcat( file, file1 );
	ofstream outstuf;
	outstuf.open( file, ios::out );
	
	if( !outstuf )
	{
		cerr << "File could not be open. " << endl;
		abort();
	}

	outstuf << " 建立存放记录的文件。 2~6行分别记录记录号、记录文件名、记录长、删除标志、记录标志 " << endl;
	
	_itoa( ((*temp1).data_num) , buffer2, 10 );      //第二行将整型转为字符
	outstuf << buffer2  << endl;					 //写入记录号
	
	outstuf << (*temp1).fname << endl;               //第三行写入记录文件名
	
	_itoa( (*temp1).blocklength , buffer3, 10 );	 //第四行将整型转为字符
	outstuf << buffer3 << endl;						 //写入记录长
    
	int a;
	if( (*temp1).deletetag==true ){ a=1; }		     //第五行
    else
	{ a=0;}										     //用0、1来表示true false
	 _itoa( a, buffer4, 10);
	outstuf << buffer4 << endl;						 //写入deletetag

	outstuf << (*temp1).recordtag << endl;           //第六行写入recordtag

	outstuf.close();								 //关闭文件    
    delete []file;
	delete []buffer1;
	delete []buffer2;
	delete []buffer3;
	delete []buffer4;
  }
  
  ofstream out;
  out.open("c:\\current.txt", ios::out);           
  //将读写指针位置写入"c:\current.txt"文件
  char *buffer5 = new char[20];     
  char *buffer6 = new char[20];
  _itoa( getread(), buffer5 ,10);       
  _itoa( getwrite(), buffer6 ,10);
  out << buffer5 << endl;			//将读指针写入
  out << buffer6 << endl;			//将写指针写入
  out.close();
  delete []buffer5;
  delete []buffer6;
  
  index();
}


void function::Modify()
//修改记录
{
	int q;
	do
	{
		q=0;
		cout << "\n修改记录标识,请输入你的选择\n";
		cout << "1---------按记录号修改\n";
		cout << "2---------修改当前写指针记录\n";
		cout << "3---------修改当前读指针记录\n";
		cout << "4---------按原来记录标志修改\n";
		cout << "0---------返回主目录\n";
		cin >> q;
		switch(q)
		{
		case 0: break;
		case 1:							//按记录号修改
			{
				int p;
				cout << "\n请输入记录号\n";
				cin >> p;
				recordmodify(p);
				break;
			}
		case 2:							//修改当前写指针记录
			{
				cout << "原记录标识是:" << (*currentwrite).recordtag << endl;
				delete [](*currentwrite).recordtag;
				(*currentwrite).recordtag = new char[50];
				strcpy( (*currentwrite).recordtag, "no record");
				cout << "\n请输入新的记录标志\n";
				cin >> (*currentwrite).recordtag;
				cout << "修改成功\n" ;
				break;
			}
		case 3:							//修改当前读指针记录
			{
				cout << "原记录标识是:" << (*currentread).recordtag << endl;
				delete [](*currentread).recordtag;
				(*currentread).recordtag = new char[50];
				strcpy( (*currentread).recordtag, "no record");
				cout << "\n请输入新的记录标志\n";
				cin >> (*currentread).recordtag;
				cout << "修改成功\n";
				break;
			}
		case 4:							//按原来记录标志修改
			{
				char *s=new char[500];
				cout << "\n请输入原记录标志\n";
                cin >> s;
				
				list<record>::iterator ptr=Listlink.begin();
				while( ptr!=Listlink.end() )
				{
					char bb[55];strcpy( bb, (*ptr).recordtag );
					if ( strcmp( (*ptr).recordtag, s )==0 )
					{
						cout << "\n请输入新的记录标志\n";
						delete [](*ptr).recordtag;
						(*ptr).recordtag = new char[50];
						strcpy( (*ptr).recordtag, "no record");
						cin >> (*ptr).recordtag;
						cout << "修改成功\n";
						break;
					}
					else
					{
						ptr++;
					}
				}
				if(ptr==Listlink.end())
				{
					cout << "输入标志不存在\n";
				}
				break;
			}
		default:break;
		}
	}
	while(q!=0);
}


void function::Append()
//加入记录
{
 int w;
 do
 {		
	w=0;
	cout << "\n加入记录,请键入你的选择\n";
	cout << "1---------加到尾部\n";
	cout << "2---------插到当前写指针之前\n";
	cout << "3---------插到指定标志号之前\n";
	cout << "4---------插到指定记录标志之前\n";
	cout << "0---------返回主目录\n";
	cin >> w;
	 switch(w)
	 {
	 case 0:break;
	 case 1:                     //加到尾部
		 {
			 char *filename = new char[255];
			 cout << "\n请输入文件路径:\n";
			 cin >> filename;

			if(!filename)
			{
				cerr << "文件无法打开" << endl;
				abort();
			}
		   else
		  {
			 filelocate( filename );
			 if( temp.data_num==-1) 
			 { 
				 cout << "输入错误\n";
				 temp.data_num=0;
				break; 
			 }
			 
			 if( currenttemp == Listlink.end() )
			 {
				if( Listlink.begin() != Listlink.end() )
				{
					currenttemp--;
				}
				 dnum++;
				 temp.data_num = dnum;
				 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.push_back(temp);     
					 if( dnum==1)
					 {
						 currentread++;
					 }
					  currentwrite++;
					 
					  cout << "成功加入尾部\n";	
					 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 << "文件已存在,是否链接(Y/N) \n";
						char ch='n';
					cin >> ch;
					if( ch=='y' || ch == 'Y' )
					{
						cout << "文件已存在,现与之链接……" << endl;
						 f.seekp(0,ios::end);
						 long pos=f.tellp();
						 f.seekp(0,ios::beg);
 						 pos-= f.tellp();
 						 temp.blocklength = pos;
 						 f.close();

						 Listlink.push_back(temp);
						  if( dnum==1)
						 {
						 currentwrite=Listlink.begin();
						 currentread = Listlink.end();
						 }
						else
						{
						 currentwrite++;
						 }
					    cout << "成功加入尾部\n";
						
						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
					{
						if( ch!='n' && ch!='N')
						{
							cout << "输入错误\n";
						}
						break;
					}
						
				 }
			 }
			 else
			 {
					 cout << "\n该文件名已在记录中存在\n";
					 break;
			 }
		 }
		 }

	 case 2:                         //插到当前写指针之前
		 {
			 getwrite();
			 char *filename = new char[255];
			 cout << "\n请输入文件路径:\n";
			 cin >> filename;
			if(!filename)
			{
				cerr << "文件无法打开" << endl;
				abort();
			}
		   else
		  {
			 filelocate( filename );
			 if( currenttemp == Listlink.end() )
			 {
				 dnum++;
				 temp.data_num = (*currentwrite).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::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( currentwrite, temp );     //写指针指向新结点
					 currentwrite--;
					 list<record>::iterator ptr=currentwrite;
					 ptr++;
					  while( ptr!=Listlink.end() )
					 {
						 (*ptr).data_num++;
						 ptr++;
					 }
					 	
					 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( currentwrite, temp );
						 currentwrite--;
			
					
					  list<record>::iterator ptr=currentwrite;
					  ptr++;
					  while( ptr!=Listlink.end() )
						 {
							 (*ptr).data_num++;
							ptr++;
						 }
						
						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 3:                          //插到指定标志号之前
		 {
			 int j;
			 cout << "\n请输入记录号:\n";
			 cin >> j;
			 locate(j);
			 if( temp.data_num==-1) 
			 { 
				 cout << "输入错误\n";
				 temp.data_num=0;
				break; 
			 }
			 list<record>::iterator apptemp=currenttemp;

			  char *filename = new char[255];
			 cout << "\n请输入文件路径:\n";
			 cin >> filename;
			 //gets(filename);
			if(!filename)
			{
				cerr << "文件无法打开" << endl;
				abort();
			}
		   else
			{
			 filelocate( filename );
			 if( currenttemp == Listlink.end() )
			 {
				 dnum++;
				 temp.data_num = (*apptemp).data_num;
			     strcpy( temp.fname, filename);
	

⌨️ 快捷键说明

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