user-0 提问: c语言文件输入输出问题，acm上一直通不过，求大神解答

回复:
user: user-1
content: #include <ctype.h> #include <stdio.h> #include <string.h> #include <stdlib.h>  #define SIZE 100  void input(char * str,FILE * fp); char *sort( const char *str1,const char * str2);  int main(void) {     char *str1, *str2, *str3;     FILE *fp;          str1 = (char *)malloc(SIZE * sizeof(char));     str2 = (char *)malloc(SIZE * sizeof(char));  	//create file A      if((fp=fopen("A", "w"))!=NULL) {         input(str1, fp);         fclose(fp);    }//not close NULL        //create file B     if((fp=fopen("B", "w"))!=NULL) {        input(str2, fp);        fclose(fp);    }     	str3 = sort(str1, str2);     //整理  	if((fp=fopen("C","w"))!=NULL) { 		fputs(str3, fp);        //写入到C文件 		fclose(fp);   }     if((fp=fopen("A", "r"))!=NULL) {      	fgets(str1,SIZE, fp);   //从A中读取     	fclose(fp);     }     if((fp=fopen("B","r"))!=NULL) {         fgets(str2,SIZE,fp);    //从B中读取        	fclose(fp); 	} 	     if((fp=fopen("C","r"))!=NULL) {         fgets(str3,SIZE,fp);    //从C中读取        	fclose(fp);     }          printf("the content of file A:");   //显示    	puts(str1);free(str1); //////////////////////free string     printf("the content of file B:");     puts(str2);free(str2);//////////////////////free string     printf("the content of file C:");     puts(str3);free(str3);//////////////////////free string //    free(fp);  ///////////////////////////////do not free file desc     return 0; } void input(char * str,FILE * fp) { 	gets(str);  	fputs(str, fp); } char *sort(const char *str1, const char * str2) {     char * str3, temp;     int i,j,min;     str3 = (char *)malloc((strlen(str1) + strlen(str2) + 1));     str3 = strcpy(str3,str1);     str3 = strcat(str3,str2);          min = strlen(str3);     for(i = 0; i < min-1; i++) //bubble sort     {     	for (j = i+1; j < min; j++)      	{     		if (str3[i] > str3[j])     		{     			temp = str3[i];     			str3[i] = str3[j];     			str3[j] = temp; 			} 		} 	}     return str3; }

user: user-1
content: 不知道原题,按照你的思路搞得,你的描述有问题

user: user-0
content: 引用 1 楼 lgbxyz 的回复: #include <ctype.h> #include <stdio.h> #include <string.h> #include <stdlib.h>  #define SIZE 100  void input(char * str,FILE * fp); char *sort( const char *str1,const char * str2);  int main(void) {     char *str1, *str2, *str3;     FILE *fp;          str1 = (char *)malloc(SIZE * sizeof(char));     str2 = (char *)malloc(SIZE * sizeof(char));  	//create file A      if((fp=fopen("A", "w"))!=NULL) {         input(str1, fp);         fclose(fp);    }//not close NULL        //create file B     if((fp=fopen("B", "w"))!=NULL) {        input(str2, fp);        fclose(fp);    }     	str3 = sort(str1, str2);     //整理  	if((fp=fopen("C","w"))!=NULL) { 		fputs(str3, fp);        //写入到C文件 		fclose(fp);   }     if((fp=fopen("A", "r"))!=NULL) {      	fgets(str1,SIZE, fp);   //从A中读取     	fclose(fp);     }     if((fp=fopen("B","r"))!=NULL) {         fgets(str2,SIZE,fp);    //从B中读取        	fclose(fp); 	} 	     if((fp=fopen("C","r"))!=NULL) {         fgets(str3,SIZE,fp);    //从C中读取        	fclose(fp);     }          printf("the content of file A:");   //显示    	puts(str1);free(str1); //////////////////////free string     printf("the content of file B:");     puts(str2);free(str2);//////////////////////free string     printf("the content of file C:");     puts(str3);free(str3);//////////////////////free string //    free(fp);  ///////////////////////////////do not free file desc     return 0; } void input(char * str,FILE * fp) { 	gets(str);  	fputs(str, fp); } char *sort(const char *str1, const char * str2) {     char * str3, temp;     int i,j,min;     str3 = (char *)malloc((strlen(str1) + strlen(str2) + 1));     str3 = strcpy(str3,str1);     str3 = strcat(str3,str2);          min = strlen(str3);     for(i = 0; i < min-1; i++) //bubble sort     {     	for (j = i+1; j < min; j++)      	{     		if (str3[i] > str3[j])     		{     			temp = str3[i];     			str3[i] = str3[j];     			str3[j] = temp; 			} 		} 	}     return str3; }      按照你的修改提交之后还是错误的

user: user-2
content: 打开或关闭文件的if语句要加{} #include <ctype.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #define SIZE 100 void input(char * str,FILE * fp); char * sort( const char *str1,const char * str2); int main() {     char * str1,*str2,*str3;     FILE * fp;     str1=(char *)malloc(SIZE*sizeof(char));     str2=(char *)malloc(SIZE*sizeof(char));     if((fp=fopen("A.txt","w"))!=NULL){         input(str1,fp);     //读取第一行并写入到A文件         fclose(fp); 	}     if((fp=fopen("B.txt","w"))!=NULL){        input(str2,fp);        //读取第二行并写入到B文件        fclose(fp); 	}     str3=sort(str1,str2);     //整理     if((fp=fopen("C.txt","w"))!=NULL){        fputs(str3,fp);        //写入到C文件        fclose(fp); 	}    if((fp=fopen("A.txt","r"))!=NULL){         fgets(str1,SIZE,fp);   //从A中读取         fclose(fp); 	}     if((fp=fopen("B.txt","r"))!=NULL){         fgets(str2,SIZE,fp);    //从B中读取         fclose(fp); 	}     if((fp=fopen("C","r"))!=NULL){         fgets(str3,SIZE,fp);    //从C中读取         fclose(fp); 	}     printf("the content of file A:");   //显示     puts(str1);     printf("the content of file B:");     puts(str2);     printf("the content of file C:");     puts(str3);     free(fp);     return 0; } void input(char * str,FILE * fp) {       gets(str);     fputs(str,fp); } char * sort(const char *str1,const char * str2) {     char * str3,temp;     int i,j,min;     str3=(char *)malloc((strlen(str1)+strlen(str2)+1)*sizeof(char));     str3=strcpy(str3,str1);     str3=strcat(str3,str2);     for(i=0;i<strlen(str3);i++)     {         min=i;         for(j=i+1;j<strlen(str3);j++)         {            if(str3[min]>str3[j])             min=j;         }         temp=str3[i];         str3[i]=str3[min];         str3[min]=temp;     }     return str3; }