c语言文件操作问题(我是菜鸟)
有两个磁盘文件“A”和“B”,各存放一行字符,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新文件“C”中去,为什么结果C文件中什么都没有,求高手指点,谢谢!代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fpA,*fpB,*fpC;
if((fpA=fopen("A.txt","rb"))==NULL)
{
printf("connot open the A file!\n");
exit(0);
}
if((fpB=fopen("B.txt","rb"))==NULL)
{
printf("connot open the B file!\n");
exit(0);
}
if((fpC=fopen("B.txt","wb"))==NULL)
{
printf("connot open the C file!\n");
exit(0);
}
int i=0,j;
char str[300]={""};
char ch;
char str1[100]={""};
fgets(str,99,fpA);
fgets(str1,99,fpB);
fclose(fpA);
fclose(fpB);
strcat(str,str1);
for(;i<199 || str[i]!='\0';i++)
{
char temp;
for(j=i+1;j<200;j++)
if(str[i]>str[j])
{
temp = str [i];
str[i]=str[j];
str[j]=temp;
}
}
printf("the string of C.file is : \n");
for(i=0;str[i]!='\0' && i<200;i++)
{
fwrite(&str[i],sizeof(char),1,fpC);
putchar(str[i]);
}
printf("\n");
return 0;
}
[解决办法]
请用归并的思想
[解决办法]
同学你打开了二次B文件