为什么我程序里的‘ i ’不对了?高手来下
程序代码:
#include<stdio.h> #include<stdlib.h> #include<conio.h> #define L 1 main() { FILE *fp; char ch[200],c; int i=0,j,n; if((fp=fopen("file1.txt","w+"))==NULL) { printf("Can't open this file"); exit(0); } printf("gets in file1:\t"); while((c=getchar())!='\n') fputc(c,fp); rewind(fp); while((c=fgetc(fp))!=EOF) ch[i++]=c; #ifdef L printf("i=%d",i); #endif fclose(fp); if((fp=fopen("file2.txt","w+"))==NULL) { printf("can't open this file"); exit(0); } printf("gets in file2:\t"); while((c=getchar())!='\n'); fputc(c,fp); rewind(fp); while((c=fgetc(fp))!=EOF) {ch[i++]=c; #ifdef L printf("i=%d",i); #endif } fclose(fp); n=i; for(i=1;i<n;i++) for(j=0;j<n;j++) if(ch[j]<ch[i]) { c=ch[j]; ch[j]=ch[i]; ch[i]=c; } if((fp=fopen("file3.txt","w+"))==NULL) { printf("can't open this file"); exit(0); } for(i=0;i<n;i++) fputc(ch[i],fp); rewind(fp); while((c=fgetc(fp))!=EOF) putchar(c); printf("\n"); fclose(fp); getch(); }我想把file1和file2合并、排序到file3中,为什么我里面的i的值有问题?