ITeye博客

从两个文件读入字母,合并排序后输出到另一个文件

zy3381 · 2014-03-17 16:20 · 算法代码收藏 · 编程语言 · Markdown

没啥多说的。。。


#include<stdio.h>
void main()
{
    FILE *fp1, *fp2, *fp3;
    char filePath1[50],filePath2[50],filePath3[50];
    char buf[250], ch;
    int i, j=0, k;
    printf("FilePath1:");
    scanf("%s", filePath1);
    printf("FilePath2:");
    scanf("%s", filePath2);
    printf("FilePath3:");
    scanf("%s", filePath3);

    if((fp1 = fopen(filePath1, "rb")) == NULL)
    {
        printf("File1 Open Error!\n");
        return;
    }
    if((fp2 = fopen(filePath2, "rb")) == NULL)
    {
        printf("File2 Open Error!\n");
        return;
    }
    if((fp3 = fopen(filePath3, "w")) == NULL)
    {
        printf("File3 Open Error!\n");
        return;
    }

    while((ch = fgetc(fp1)) != EOF)
    {
        buf[j++] = ch;
    }

    while((ch = fgetc(fp2)) != EOF)
    {
        buf[j++] = ch;
    }

    for(i=0; i<j; i++)
    {
        for(k=i+1; k<j; k++)
        {
            if(buf[i]>buf[k])
            {
                ch = buf[i];
                buf[i] = buf[k];
                buf[k] = ch;
            }
        }
        fputc(buf[i], fp3);
    }

    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
    printf("Finish!\n");
}


评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。