C语言自制文件分割器(简单)

来源:互联网 发布:干支纪日法简便算法 编辑:程序博客网 时间:2024/06/12 01:26

记得初中的时候用MP3看电子书,MP3支持的文本文档格式不能超过1M,否则打不开,然后在网上下了一个文件分割器,感觉真的很强大啊(别吐槽,当时真的很白)。

等自己学了C语言后,便想自己弄一个文件分割器,可是在网上搜了一下源码,看起来很复杂的样子,也就没了兴趣。现在又重新拾起了这个愿望,不过第一次弄的比较简单,没有重命名等功能,以后还会重新附加功能的。

有什么不足的,欢迎拍砖。

闲话少叙,直接上源代码。

/* copy the file1 into 2 file -------- * file2 && file3 * And then print file2 && file3 */#include <stdio.h>#include <stdlib.h>int main(void){FILE *fp1 ,*fp2,*fp3;int ch;int row_count = 0; //count the rowsif ( (fp1 = fopen ("file1","r+")) == NULL) {perror ("open file file1\n");exit(1);}if ( ( fp2 = fopen ("file2", "w+")) == NULL) {perror ("open file file2\n");exit(1);}if ( ( fp3 = fopen ("file3", "w+")) == NULL) {perror ("open file file3\n");exit(1);}//count the row of the filewhile ((ch = fgetc (fp1)) != EOF ){if (ch == '\n') row_count++;}rewind(fp1);//copy file1 into two files----file2 && file3while ((ch = fgetc (fp1)) != EOF){static int copy_row_count = 0;if (ch == '\n') copy_row_count++;if (copy_row_count <= row_count/2) {fputc (ch, fp2); continue;}if (copy_row_count >  row_count/2) {fputc (ch, fp3); continue;}}rewind(fp1);rewind(fp2);rewind(fp3);printf("#########\nfile1:\n#########\n");while ((ch = fgetc(fp1)) != EOF )putchar(ch);printf("#########\nfile2:\n#########\n");while ((ch = fgetc(fp2)) != EOF )putchar(ch);printf("#########\nfile3:\n#########\n");while ((ch = fgetc(fp3)) != EOF )putchar(ch);fclose(fp1);fclose(fp2);fclose(fp3);return 0;}

欢迎大家指正批评!!



原创粉丝点击