[英]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? 如果我是对的,则以下代码应将两个文件合并为一个新文件(newcat),但是如何使用strcmp函数将字符串按字母顺序“排序”?
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. 对于每个文件,您可以考虑每个字符串,然后使用strcmp
使用您选择的任何排序算法比较字符串,并将结果放在目标文件中。 That will merge the files in alphabetical order. 这将按字母顺序合并文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.