简体   繁体   中英

Merging text files in C

I've been trying to make a function to merge two text files together into a new file which is then sorted alphabetically. If i'm right then the following code should combine the two files into a new file (newcat) but how can I use the strcmp function to "sort" the strings into alphabetical order?

    void combine(FILE* cat1, FILE* cat2, FILE* newcat)
    {
       char ch;

    while((ch = fgetc(cat1)) != EOF)
       fputc(ch,newcat);

    while((ch = fgetc(cat2)) != EOF)
       fputc(ch,newcat);

    fclose (cat1);
    fclose (cat2);
    fclose (newcat);
    }

For each file you can consider each string and then using strcmp compare strings using any sorting algorithm of your choice and place the result on the destination file. That will merge the files in alphabetical order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM