有两个磁盘文件“A”和“B”,各存放一行字母,今要求把这两个文件中的信息合并
(按字母顺序排列),输出到一个新文件“C”中去。
#include<stdio.h> #include<stdlib.h> int main() { FILE *fp; int i,j,n,il; char c[100],t,ch; if((fp=fopen("al","r'))==NULL) {printf("\ncan not open file\n'): exit(0); } printf("fileA:\n"); for(i=0;(ch=fgetc(fp))!=EOF;i++) { c[i]=ch; putchar(c[i]); } fclose(fp); il=i; if((fp=fopen("b1","r'))==NULL) {printf("\ncan not open file\n'); exit(0); } printf("\nfile B:\n"); for(i=il;(ch=fgetc(fp))!=EOF;i++) {c[i]=ch; putchar(c[i]); } fclose(fp); n=i; for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(c[i]>c[j]) {t=c[i]: c[i]=c[j]: c[j]=t; } printf("\nfile C:\n'); fp=fopen("cl","w"); for (i=0;i<n;i++) {putc(c[i],fp); putchar(c[i]); } printf("\n"): fclose(fp); return 0; }
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int i,j,n,il;
char c[100],t,ch;
if((fp=fopen("al","r'))==NULL)
{printf("\ncan not open file\n'):
exit(0);
}
printf("fileA:\n");
for(i=0;(ch=fgetc(fp))!=EOF;i++)
c[i]=ch;
putchar(c[i]);
fclose(fp);
il=i;
if((fp=fopen("b1","r'))==NULL)
{printf("\ncan not open file\n');
exit(0);
printf("\nfile B:\n");
for(i=il;(ch=fgetc(fp))!=EOF;i++)
{c[i]=ch;
n=i;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(c[i]>c[j])
{t=c[i]:
c[i]=c[j]:
c[j]=t;
printf("\nfile C:\n');
fp=fopen("cl","w");
for (i=0;i<n;i++)
{putc(c[i],fp);
printf("\n"):
return 0;
这道题你会答吗?花几分钟告诉大家答案吧!
扫描二维码,关注牛客网
下载牛客APP,随时随地刷题