磁盘读写
有两个磁盘文件,各存放一行字母,今要求把这两个文件中的信息合并(按字母顺序排列),输出到一个新的文件中去。
#include <stdio.h> #include <stdlib.h> int main() { FILE* fp; //文件1 if ((fp = fopen("E:\\test\\text\\text1.txt","r")) == NULL) { printf("can not open file\n"); exit(0); } char ch; char a[100]; int i; for (i = 0; (ch = fgetc(fp)) != EOF; i++) { a[i] = ch; } //文件2 if ((fp = fopen("E:\\test\\text\\text2.txt", "r")) == NULL) { printf("can not open file\n"); exit(0); } for (; (ch = fgetc(fp)) != EOF; i++) { a[i] = ch; } //排序 char temp; for (int j = 0; j < i; j++) { for (int k = j+1; k < i; k++) { if (a[k]<a[j]) { temp = a[j]; a[j] = a[k]; a[k] = temp; } } } //写入文件3 if ((fp = fopen("E:\\test\\text\\text3.txt", "w")) == NULL) { printf("can not open file\n"); exit(0); } for (int n=0;n<i; n++) { putc(a[n], fp); } return 0; }