使用说明:
       

    一:设计之初没考虑到“已存在文件进行追加”的功能(有兴趣的童鞋可以试试),所以运行程序第一步只能是先选“1”,新建一份表单:
标准样例(名字:三个中文字符,学号:12位数字,性别:男/女,年龄:两位整数)如下:(可以直接复制)

//名字纯粹乱打的哈QAQ

王二哈 123123123123 男 28
李边牧 111111111111 男 08
赵师石 100100100100 男 35
李龙龙 123456789123 男 48
蒋石石 122222207854 男 91
杨慧慧 255555454444 女 34

    二:运行程序的顺序为:

  1. 运行程序,选1,复制样例,创建表单。(不需要事先在D盘创建“table.txt"文件)
  2. 第1步进行之后,就可以选择去“查询”、“修改”、“删除”或者“排序”操作,每次操作后都可以打开D盘的table.txt查看操作后的结果。
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<string>
#include<string.h>
#include<cstdlib> 
using namespace std; 
FILE *fp;  
struct stu{
        char name[7];      //注意,输入格式一定要按这个格式 
        char ID[13];       //请按姓名(3位) 学号(12位) 性别(1位) 年龄(2位)
        char sex[3];      //男,女 
        char age[3];      //两位数字 
        char tp='\n';     //为了表单更好看,末位加个回车. 
}*p;
class student{          //修改n次的版本(填以前的坑)
     private:
        stu stud[40];
        stu stud1[40];
     public:
     	student(){};
		~student(){}; 
         int num=0;
        void addinf();      //增加信息  1     //inf:information
        void showinf();     //显示信息  2     // 
        void seacinf();     //查找信息  3
        void modify();      //修改信息  4
        void delinf();      //删除信息  5      
        void sortinf();     //信息排序  6
        void menu();        //功能主菜单
        void screen();      //界面停留 
};
 
void student::addinf(){
	        cout<<"请输入添加学生的数量"<<endl<<endl;
	        cin>>num;
	        cout<<"请按姓名、学号、性别、年龄的顺序输入信息"<<endl<<endl; 
	    	for(int i=0;i<num;i++){
	        	cin>>stud[i].name>>stud[i].ID>>stud[i].sex>>stud[i].age;
		    }
	 		fp=fopen("D:\\table.txt","w+");
				if(fp==NULL){
		 			cout<<"文件打开失败!"<<endl;
		        	screen();
				}
	  		for(int i=0;i<num;i++){
	  			fwrite(&stud[i],sizeof(struct stu),1,fp);
		    }
		    fclose(fp);
		    //rewind(fp);//文件指针归位 
		    getchar();
	        cout<<"输入完毕,请按回车键继续"<<endl<<endl; 
		    getchar();
		    menu();
}
void student::showinf(){
		fp=fopen("D:\\table.txt","a+");
			if(fp==NULL){
	 		 	cout<<"文件打开失败或未创建文件!"<<endl;
	 		 	fclose(fp);
	         	screen();
			}
			if(num==0&&feof(fp)==1){
				cout<<"没有新建储存信息,请添加后再进行操作!!!"<<endl;
				fclose(fp);
				screen(); 
			}
			else if(num){
			    for(int i=0;i<num;i++){
				    fread(&stud1[i],sizeof(struct stu),1,fp);
			        cout<<stud1[i].name<<" "<<stud1[i].ID<<" "<<stud1[i].sex<<" "<<stud1[i].age<<endl<<endl;
				}
				fclose(fp);
				screen();
		    }
		    else if(!feof(fp)&&num==0){   //已存在table.txt文件 ,直接读取 
		    	int ff=0;
		    	while(!feof(fp)){
		    		ff=fgetc(fp);
		    		if(ff=='\n'){
		    			num++;
					}
				}
				rewind(fp);
				for(int i=0;i<num;i++){
				    fread(&stud1[i],sizeof(struct stu),1,fp);
			        cout<<stud1[i].name<<" "<<stud1[i].ID<<" "<<stud1[i].sex<<" "<<stud1[i].age<<endl<<endl;
				}
				fclose(fp);
				screen();
			}
 }
void student::seacinf(){
	 	fp=fopen("D:\\table.txt","r+");
			if(fp==NULL){
		 		cout<<"文件打开失败!"<<endl;
		        screen();
			}
	        int flag=0,l;
	        char  s[12];   //可以添加从姓名,年龄来找 
	        cout<<"请输入要查找学生的学号"<<endl<<endl;
			for(int i=0;i<12;i++)
	               cin>>s[i];
	   			for(int i=0;i<num;i++){
	    			fread(&stud1[i],sizeof(struct stu),1,fp);
	    			if(strcmp(s,stud1[i].ID)==0){
	        			flag=1;
	       				 l=i;
	    			}
	  			}		
		  	 if(!flag){
		        cout<<"没有该学生的信息"<<endl<<"请按回车键继续"<<endl<<endl;
		        fclose(fp);
		        screen();
		  	 }
		   if(flag==1){  
		     	cout<<"找到了!!!"<<endl; 
		   	    cout<<stud1[l].name<<" "<<stud1[l].ID<<" "<<stud1[l].sex<<" "<<stud1[l].age<<endl;
		   	    fclose(fp);
		   	    screen();
		   }
}
void student::modify(){
	 	fp=fopen("D:\\table.txt","r+");
			if(fp==NULL){
	 		 	cout<<"文件打开失败!"<<endl;
	 		 	fclose(fp);
	        	screen();
			}
	        int flag=0,l,n;
	        char  s[12];
	        cout<<"请输入要修改学生的学号"<<endl<<endl;
			for(int i=0;i<12;i++)
	               cin>>s[i];
   			for(int i=0;i<num;i++){ 
		    	fread(&stud1[i],sizeof(struct stu),1,fp);
			}
		    for(int i=0;i<num;i++){
				if(strcmp(s,stud1[i].ID)==0){
			        flag=1;
			        l=i;
			    }
		    }
		   if(!flag){
		        cout<<"没有该学生的信息"<<endl<<"请按回车键继续"<<endl<<endl;
		        fclose(fp);
		        screen();
		   }
		   if(flag==1){   
		   	    cout<<stud1[l].name<<" "<<stud1[l].ID<<" "<<stud1[l].sex<<" "<<stud1[l].age<<endl;
		   	    cout<<"是否要修改该信息"<<endl<<"1:是  "<<endl<<"0:否"<<endl;
				   cin>>n;
				   if(n==0) 
		   	       screen();
		   	       if(n==1){
		   	       	    cout<<"请按姓名(3位) 学号(12位) 性别(1位) 年龄(2位)(中间以空格隔开)的顺序输入修改后的信息"<<endl;
		   	       	 	cin>>stud1[l].name>>stud1[l].ID>>stud1[l].sex>>stud1[l].age;
		   	       	 	rewind(fp);
		   	       	 	for(int i=0;i<num;i++){
		  					 fwrite(&stud1[i],sizeof(struct stu),1,fp); //重新读入一次 
			  		 	}
		   	       	 	cout<<"修改完毕!"<<endl; 
				  }
				  fclose(fp);
		   		  screen();
		   }
		   
}
void student::delinf(){	    
	        int flag=0,l,n;
	        char  s[12];
	        cout<<"请输入要删除信息学生的学号"<<endl<<endl;
			for(int i=0;i<12;i++)
	               cin>>s[i];
			fp=fopen("D:\\table.txt","r+"); 
			if(fp==NULL){
	 		    cout<<"文件打开失败!"<<endl;
	            screen();
			}
		    for(int i=0;i<num;i++){ 
		    	fread(&stud[i],sizeof(struct stu),1,fp);
				if(strcmp(s,stud[i].ID)==0){
			        flag=1;
			        l=i;
			    }
			}
			   if(!flag){
			        cout<<"没有该学生的信息"<<endl<<"请按回车键继续"<<endl<<endl;
			        fclose(fp);
			        screen();
			   }
		       else{   
		   	    	cout<<stud[l].name<<" "<<stud[l].ID<<" "<<stud[l].sex<<" "<<stud[l].age<<endl;
		   	    	cout<<"是否要删除该信息"<<endl<<"1:是  "<<endl<<"0:否"<<endl;
				   	cin>>n;
				   	if(!n){
					    fclose(fp);
						screen();
					} 
		   	      	else{
		   	       
				   	   	for(int i=l;i<num-1;i++){ //把要删除的用后面的覆盖
				  			strcpy(stud[i].name,stud[i+1].name);
			   		  	  	strcpy(stud[i].ID,stud[i+1].ID);
			   		  	 	strcpy(stud[i].sex,stud[i+1].sex);
			   		   		strcpy(stud[i].age,stud[i+1].age);
					  	} 
					    num=num-1;
					    
					    FILE *kp=fopen("D:\\temp.txt","w+");
					  	for(int i=0;i<num;i++){
					  		 fwrite(&stud[i],sizeof(struct stu),1,kp); //重新读入一次 
						} 	
						fclose(kp);   //切记想要进行rename()或者remove()操作,一定要先关闭文件!!!!! 
						fclose(fp);
						if(remove("D:\\table.txt")==0){
							cout<<"即将删除成功,请按enter键继续"<<endl;
							getchar();
							getchar();
						}
						if(rename("D:\\temp.txt","D:\\table.txt")==0){
							 cout<<"删除成功!"<<endl; 
						} 	
				   	    else perror("rename");	
				   }
			    }
		  		screen();
}
bool cmp(stu a,stu b){          //愚笨的暴力排序 
	if(a.ID[0]==b.ID[0]){    
		if(a.ID[1]==b.ID[1]){
			if(a.ID[2]==b.ID[2]){
				if(a.ID[3]==b.ID[3]){
					if(a.ID[4]==b.ID[4]){
					    if(a.ID[5]==b.ID[5]){
					    	if(a.ID[6]==b.ID[6]){
					    		if(a.ID[7]==b.ID[7]){
					    			if(a.ID[8]==b.ID[8]){
					    				if(a.ID[9]==b.ID[9]){
					    					if(a.ID[10]==b.ID[10]){
					    					return a.ID[11]<=b.ID[11];
										}return a.ID[10]<b.ID[10];
									}return a.ID[9]<b.ID[9];
								}return a.ID[8]<b.ID[8];
							}return a.ID[7]<b.ID[7];
						}return a.ID[6]<b.ID[6];
					}return a.ID[5]<b.ID[5];
				}return a.ID[4]<b.ID[4];
			}return a.ID[3]<b.ID[3];
		}return a.ID[2]<b.ID[2];	
	}return a.ID[1]<b.ID[1];
}return a.ID[0]<b.ID[0];
}
void student::sortinf(){
	       int y;
	 		fp=fopen("D:\\table.txt","r+");
			if(fp==NULL){
	 			cout<<"文件打开失败!"<<endl;
	        	screen();
			}
			for(int i=0;i<num;i++){ 
		    	fread(&stud[i],sizeof(struct stu),1,fp);
		   	}
	    	rewind(fp);  
			sort(stud,stud+num,cmp);
			for(int i=0;i<num;i++){ 
	    		fwrite(&stud[i],sizeof(struct stu),1,fp);
	   		}
		   	cout<<"排序完成,请按2查看!!"<<endl;	    
		  	fclose(fp); 
		  	screen();
}
void student ::screen(){
 		getchar();
 		getchar();
 		menu();
} 
 
void student::menu()
{
 	system("cls");
 	   cout<<"             ---------学生信息管理系统---------              "<<endl<<endl;
	   cout<<"                1 : 增加信息     4 : 修改信息                "<<endl<<endl;	 
 	   cout<<"                2 : 显示信息     5 : 删除信息                "<<endl<<endl;
 	   cout<<"                3 : 查找信息     6 : 信息排序                "<<endl<<endl;
 	   cout<<"                0 : 退出程序                                 "<<endl;
 	   cout<<"             ----------------------------------              "<<endl<<endl;
 	   cout<<"请选择(0——6)"<<endl; 
      int chioce;
      cin>>chioce;
      switch(chioce){
	      case 1: addinf(); break;
	      case 2: showinf();break;    
	      case 3: seacinf();break;
	      case 4: modify(); break;     
	      case 5: delinf(); break;     
	      case 6: sortinf();break;
	      case 0: exit(0);            //直接退出 
	      default : cout<<"选择错误!!!请重新选择 "<<endl;screen(); 
      }
}
int main(){
    student ss;
    ss.menu();
    return 0;
}