user-0 提问: 下面的文件操作哪里出错了？急求解答，谢谢了

回复:
user: user-1
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-0
content: int get_run(LoserTree &ls,WorkArea &wa,int &rmax,int rc,FILE * &fi,FILE * &fo){     //求得一个初始归并段，fi为输入文件指针，f0为输出文件指针     int q,minimax;     while(wa[ls[0]].rnum==rc){//选得的MINIMAX记录属当前段时         q=ls[0];//q指示MINIMAX记录在wa中的位置         minimax=wa[q].key;         if(!fwrite(&wa[q].rec,sizeof(ElemType),1,fo)){//将刚选好的MINIMAX记录写入输出文件             cout<<"Fail to write data!"<<endl;             return ERROR;         }//if         if(feof(fi)){wa[q].rnum=rmax+1;wa[q].key=MAXKEY;}//输入文件结束，虚设记录（属”rmax+1"段）         else{             if(!fread(&wa[q].rec,sizeof(ElemType),1,fi)){//从输入文件读入下一记录                 cout<<"Fail to read data!"<<endl;                 return ERROR;             }//if             wa[q].key=wa[q].rec.key;//提取关键字             if(wa[q].key<minimax){//新读入的记录属下一段                 rmax=rc+1;wa[q].rnum=rmax;             }             else wa[q].rnum=rc;//新读入的记录属当前段         }         Select_MinMax(ls,wa,q);//选择新的MINIMAX记录     }//while     return OK; }//get_run  int Replace_Selection(LoserTree &ls,WorkArea &wa,FILE * &fi,FILE * &fo){     //在败者树ls和内存工作区wa上用置换-选择排序求初始归并段，fi为输入文件（只读文件）指针，fo为输出文件（只写文件）指针，两个文件均已打开     int rc,rmax;     if(!Construct_Loser(ls,wa,fi)){//初建败者树         cout<<"Fail to construct the loser_tree!"<<endl;         return ERROR;     }//if     rc=rmax=1;//rc指示当前生成的初始归并段的段号，rmax指示wa中的关键字所属初始归并段的最大段号     while(rc<=rmax){//“rc=rmax+1"标志输入文件的置换——选择排序已完成         if(!get_run(ls,wa,rmax,rc,fi,fo)){//求得一个初始归并段             cout<<"Fail to get initial merge section!"<<endl;             return ERROR;         }//if         if(!fwrite("*",sizeof(ElemType),1,fo)){//将段结束标志写入输出文件             cout<<"Fail to write symbol in the end!"<<endl;             return ERROR;         }//if         rc=wa[ls[0]].rnum;//设置下一段的段号     }     return OK; }//Replace_Selection int Replace_Selection_author(){     LoserTree ls;     WorkArea wa;     FILE * fi,*fo;     int i,a[LIST_INIT_SIZE-1];     fi=fopen("e:\\before_sort","rb");     fo=fopen("e:\\before_sort","wb");     //for(i=1;i<L.length;i++){     for(i=1;i<L.length;i++) 		cout<<L.r[i].key<<" ";     cout<<endl;     cout<<L.r+1<<endl;     i=fwrite(&L.r[1],sizeof(ElemType),L.length-1,fo);     cout<<i<<endl;         if(!i){//将待排数据写入文件             cout<<"Fail to write data to file before sorted!"<<endl;             return ERROR;         }//if     //}     //while(!feof(fi)){     cout<<a<<endl;         if(!fread(a,sizeof(ElemType),L.length-1,fi)){//从输出文件读入下一记录             cout<<"Fail to read data from file before sort!"<<endl;             return ERROR;         }//if     //}     cout<<"The list before sort is:";     for(i=1;i<L.length;i++) 		cout<<a[i]<<" ";     cout<<endl;     fo=fopen("e:\\after_sort.txt","at+");     if(!Replace_Selection(ls,wa,fi,fo)){         cout<<"Fail to sort data by Replace_Selection!"<<endl;         return ERROR;     }     fi=fopen("e:\\after_sort.txt","at+");     i=0;     cout<<"The list after sort is:";     while(!feof(fi)){         if(!fread(a+(i++),sizeof(ElemType),1,fi)){//从输出文件读入下一记录             cout<<"Fail to read data from file sorted!"<<endl;             return ERROR;         }//if         cout<<a[i]<<" ";     }     cout<<endl;     fclose(fi);     fclose(fo);     return OK; }//Replace_Selection_author int Sort_List(){     int choice=0;     while(1){         cout<<endl;         cout<<"*****************************************************"<<endl;         cout<<"*1、Sort the list by quick_sort created by author*"<<endl;         cout<<"*2、Sort the list by quick_sort created by system*"<<endl;         cout<<"*3、Sort the list by substitute_choice_sort created by author*"<<endl;         cout<<"*4、Sort the list by substitute_choice_sort created by system*"<<endl;         cout<<"*5、Return to main menu*"<<endl;         cout<<"*****************************************************"<<endl;         cout<<"please enter your choice(1~5):";         cin>>choice;         switch(choice){             case 1:if(!QuickSort())cout<<"Fail to sort the list by quick_sort created by author!"<<endl;break;             case 2:if(!QuickSort_system())cout<<"Fail to sort the list by quick_sort created by system!"<<endl;break;             case 3:if(!Replace_Selection_author())cout<<"Fail to sort the list by substitute_choice_sort created by author!"<<endl;break;             case 4:if(!QuickSort())cout<<"Fail to sort the list by substitute_choice_sort created by system!"<<endl;break;             case 5:return OK;             default:cout<<"Input Error!Please enter the integer between 1 and 5."<<endl;         }     } 	return OK; } /*int quiksort(SqList L){//快速排序     int choice=0;     cout<<endl;     cout<<"*****************************************************"<<endl;     cout<<"* 1、Use the quicksort by author       2、Use the quicksort by system*"<<endl;     cout<<"*****************************************************"<<endl;     cout<<"please enter your choice(1~2):";     while(1){         cin>>choice;         case 1:if(!QuickSort(L))cout<<"Fail to;     } }*/  int main(){     int i,choice,a[LIST_INIT_SIZE-1]={2,3,4,9,8,5,6,1,7,10}; 	ElemType e;     if(InitList_Sq()<1){         printf("Fail to initialize the list!\n");         return ERROR;     }//if     L.length=1;     for(i=2;i<=LIST_INIT_SIZE;i++){         e.key=a[i-2];         if(ListInsert_Sq(i,e)<1)return ERROR;     } 	while(1){         initialization();//输出主菜单         cin>>choice;         switch(choice){         case 1:             if(!initialize_list())//输入待排数据                 cout<<"Fail to create the list!"<<endl;             break;         case 2:             Show_List();//输出待排数据             break;         case 3:             Sort_List();//对数据进行排序             break;         case 4:             cout<<"Thank you for your use.Hope you use our system again!"<<endl;             return 0;         default:             cout<<"Input Error!Please enter 1~4";             break;         } 	} 	return 0; }

user: user-0
content: //整体存储用的是SqList，虽然此程序key为int,用数组存储比较方便，但考虑到以后数据的复杂性，还是用结构体比较好 #include<stdio.h> #include<stdlib.h> #include<iostream> #include<list> #include<algorithm> using namespace std; #define LIST_INIT_SIZE 11//线性表存储空间的初始分配量 #define LISTINCREMENT 10 #define ERROR 0 #define OK 1 #define OVERFLOW -1 #define w 5 #define MAXKEY 1000 typedef struct{     int key; }ElemType; typedef struct{     ElemType *r;//存储空间基址     int length;//当前长度     int listsize;//当前分配的存储容量（以sizeof(ElemType)为基本单位） }SqList; SqList L; typedef struct{     ElemType rec;//记录     int key;//从记录中抽取的关键字     int rnum;//所属归并段的段号 }RcdNode,WorkArea[w];//内存工作区，容量为w typedef int LoserTree[w]; int InitList_Sq(){//构造空顺序存储的线性表 	L.r=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType)); 	if(!L.r)exit(OVERFLOW); 	L.length=0; 	L.listsize=LIST_INIT_SIZE; 	return OK; }//InitList_L int ListInsert_Sq(int i,ElemType e){     //在顺序线性表L中第i个位置之前插入新的元素e,i的合法值为1<=i<=L.length+1     int p,q;     ElemType* newbase;     if(i<1||i>L.length+1)return ERROR;//i值不合法     if(L.length>=L.listsize){//当前存储空间已满，增加分配         newbase=(ElemType *)realloc(L.r,(L.listsize+LISTINCREMENT)*sizeof(ElemType));         if(!newbase)exit(OVERFLOW);//存储分配失败         L.r=newbase;//新基址         L.listsize+=LISTINCREMENT;//增加存储容量     }     for(p=L.length-1,q=i-1;p>=q;--p)L.r[p+1]=L.r[p];//插入位置及之后的元素右移     L.r[q]=e;//插入e     ++L.length;//表长增1     return OK; }//ListInsert_Sq int Partition(int low,int high){     //交换顺序表L中子表L.r[low..high]的记录，使枢纽记录到位，并返回其所在位置，此时在它之前（后）的记录金不大（小）于它。     int pivotloc;     L.r[0]=L.r[low];//用子表的第一个记录作为枢纽记录     pivotloc=L.r[low].key;//枢纽记录关键字     while(low<high){//从表的两端交替地向中间扫描         while(low<high&&L.r[high].key>=pivotloc)--high;         L.r[low]=L.r[high];//将比枢纽记录小的记录交换到低端         while(low<high&&L.r[low].key<=pivotloc)++low;         L.r[high]=L.r[low];//将比枢纽记录大的记录交换到高端     }//while     L.r[low]=L.r[0];//枢纽记录到位     return low;//返回枢纽所在位置 }//Partition int QSort(int low,int high){     //对顺序表L中的子序列L.r[low.high]作快速排序     int pivotloc;     if(low<high){         pivotloc=Partition(low,high);//将L.r[low..high]一分为二         if(!QSort(low,pivotloc-1))return ERROR;//对低子表递归排序，pivotloc是枢纽位置         if(!QSort(pivotloc+1,high))return ERROR;//对低子表递归排序，pivotloc是枢纽位置     }//if     return OK; }//QSort int QuickSort(){     //对顺序表L作快速排序     int a[LIST_INIT_SIZE],i;     for(i=1;i<LIST_INIT_SIZE;i++)         a[i]=L.r[i].key;     if(!QSort(1,L.length-1))return ERROR;     printf("The list after sort is:");     for(i=1;i<LIST_INIT_SIZE;i++)         printf("%d ",L.r[i].key);     printf("\n");     for(i=1;i<LIST_INIT_SIZE;i++)         L.r[i].key=a[i];     return OK; }//QuickSort  //初始化系统界面 void initialization(){     cout<<endl;     cout<<"                 Welcome to Sort System"<<endl;     cout<<"*****************************************************"<<endl;     cout<<"* 1、Initialize the list           2、Show the list *"<<endl;     cout<<"* 3、Sort the list               4、Quit the game *"<<endl;     cout<<"*****************************************************"<<endl;     cout<<"please enter your choice(1~4):";     return; }  int initialize_list(){//输入待排数据     int i;     ElemType e;     printf("Please enter %d integers between 1 and 100(Example:1 2 3······):\n",LIST_INIT_SIZE-1);     L.length=1;     for(i=2;i<=LIST_INIT_SIZE;i++){         scanf("%d",&e.key);         if(ListInsert_Sq(i,e)<1)return ERROR;     }     printf("Success to initialize the list!\nThe list is ");     for(i=1;i<LIST_INIT_SIZE;i++)         printf("%d ",L.r[i].key);     return OK; }//initialize_list  int Show_List(){     int i;     printf("The list is ");     for(i=1;i<LIST_INIT_SIZE;i++)         printf("%d ",L.r[i].key);     printf("\n");     return OK; }  int QuickSort_system(){ 	//list<int>mylist; 	//list::vietor 	int i,a[LIST_INIT_SIZE]; 	//printf("The list before sort is:"); 	for(i=1;i<L.length;i++) 		a[i]=L.r[i].key; 		//mylist.push_back(L.r[i].key); 	//sort(mylist.begin(),mylist.end()); 	sort(a+1,a+L.length); 	cout<<"The list after sort is:"; 	for(i=1;i<L.length;i++) 		cout<<a[i]<<" "; 	cout<<endl; 	return OK; }  void Select_MinMax(LoserTree &ls,WorkArea wa,int q){     //从wa[q]起到败者树的根比较选择MINIMAX记录，并由q指示它所在的归并段     int t,p,temp;     for(t=(w+q)/2,p=ls[t];t>0;t=t/2,p=ls[t])         if(wa[p].rnum<wa[q].rnum||wa[p].rnum==wa[q].rnum&&wa[p].key<wa[q].key){             temp=q;q=ls[t];ls[t]=temp;//q指示新的胜利者         }     ls[0]=q; }//Select_MinMax int Construct_Loser(LoserTree &ls,WorkArea &wa,FILE * &fi){     //输入w个记录到内存工作区wa,建得败者树ls,选出关键字最小的记录并由s指示其在wa中的位置     int i;     for(i=0;i<w;i++)         wa[i].rnum=wa[i].key=ls[i]=0;//工作区初始化     for(i=w-1;i>=0;--i){         if(!fread(&wa[i].rec,sizeof(ElemType),1,fi)){//输入一个记录             cout<<"Fail to read data!"<<endl;             return ERROR;         }//if         wa[i].key=wa[i].rec.key;//提取关键字         wa[i].rnum=1;//其段号为“1”         Select_MinMax(ls,wa,i);//调整败者     }//for     return OK; }//Construct_Loser

user: user-2
content: 需要看下完整代码？