---
title: "从两个文件读入字母，合并排序后输出到另一个文件"
author: "zy3381"
date: "2014-03-17T16:20:12Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2014-03-17T16:20:12Z"
id: 2032364
url: "https://zy3381.iteye.com/blog/2032364"
---

没啥多说的。。。

```c

#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");
}


```