Spliter

    xiaoxiao2021-03-25  225

    Spliter

    可实现文件的分割与合并

    #include <stdio.h> #include <stdlib.h> #include <string.h> int Choice(); void Divide(FILE *fp, int size); void Merge(FILE *fp, int num); int main(int argc, char *argv[]) { int m = Choice(); if(m == 1) { if(argc != 3) { printf("Para must be 3 numbers!\n"); exit(0); } FILE *fp; fp = fopen(argv[1], "rb"); if(fp == NULL) { printf("open %s file failed!\n", argv[1]); exit(0); } int size = atoi(argv[2]); Divide(fp, size); fclose(fp); } else { FILE *fp; fp = fopen(argv[1], "wb");//合并后的文件名 if(fp == NULL) { printf("Open file failed!\n"); exit(0); } int num = atoi(argv[2]);//相应的文件个数 Merge(fp, num); fclose(fp); } return 0; } int Choice() { int m; printf("Welcome come to the V1.0 FILE OPERATE machine!\n"); printf("\n---------------------------------------------\n"); printf("Please input your choice!\n\n"); printf("\t 1.The divide operate!\t \n"); printf("\t 2.The merge operate!\t \n"); while(1) { scanf("%d", &m); if(m!=1 && m!=2) { printf("Please input legal data!\n"); continue; } break; } return m; } void Divide(FILE *fp, int size) { char partname[100]; char ch; int flag = 0; for(int a=1; ;a++) { itoa(a, partname, 10); strcat(partname, ".part"); FILE *out; out = fopen(partname, "wb"); if(out == NULL) { printf("Open %s file failed", partname); exit(0); } for(int i=1; i<=size; i++) { fread(&ch, sizeof(char), 1, fp); if(feof(fp)) { flag = 1; break; } fwrite(&ch, sizeof(char), 1, out); } fclose(out); if(flag == 1) break; } printf("Congratulate you've divide the file!\n"); } void Merge(FILE *fp,int num) { char partname[100]; char str[100]; for(int a=1; a<=num; a++) { itoa(a, partname, 10); strcat(partname, ".part"); FILE *out; out = fopen(partname, "rb"); if(out == NULL) { printf("Open %s file failed\n", partname); exit(0); } while(1) { int r = fread(str, sizeof(char), 100, out); if(r<1) break; fwrite(str, sizeof(char), r, fp); } fclose(out); } printf("Congratulate you've merge the file!\n"); }

    改编后的程序无需手动输入数量,只需手动输入目录txt名

    #include <stdio.h> #include <stdlib.h> #include <string.h> int Choice(); void Divide(FILE *fp, int size, FILE *div); void Merge(FILE *fp, FILE *di); int main(int argc, char *argv[]) { int m = Choice(); if(m == 1) { if(argc != 4) { printf("Para must be 3 numbers!\n"); exit(0); } FILE *fp, *div; fp = fopen(argv[1], "rb"); div = fopen(argv[3], "wb");//用于生成目录 if(fp == NULL || div == NULL) { printf("open file failed!\n"); exit(0); } int size = atoi(argv[2]);//指定分割文件的大小 Divide(fp, size, div); fclose(div); fclose(fp); } else { FILE *fp, *di; fp = fopen(argv[1], "wb");//合并后的文件名 di = fopen(argv[2], "rb");//目录 if(fp == NULL || di == NULL) { printf("Open file failed!\n"); exit(0); } Merge(fp, di); fclose(di); fclose(fp); } return 0; } int Choice() { int m; printf("Welcome come to the V2.0 FILE OPERATE machine!\n"); printf("\n---------------------------------------------\n"); printf("Please input your choice!\n\n"); printf("\t 1.The divide operate!\t \n"); printf("\t 2.The merge operate!\t \n"); while(1) { scanf("%d", &m); if(m!=1 && m!=2) { printf("Please input legal data!\n"); continue; } break; } return m; } void Divide(FILE *fp, int size, FILE *div) { char partname[100]; char ch; int flag = 0; for(int a=1; ;a++) { itoa(a, partname, 10); strcat(partname, ".part"); FILE *out; out = fopen(partname, "wb"); if(out == NULL) { printf("Open file failed\n"); exit(0); } for(int i=1; i<=size; i++) { fread(&ch, sizeof(char), 1, fp); if(feof(fp)) { flag = 1; break; } fwrite(&ch, sizeof(char), 1, out); } fprintf(div,"%s\r\n", partname); fclose(out); if(flag == 1) break; } printf("Congratulate you've divide the file!\n"); } void Merge(FILE *fp, FILE *di) { char partname[100]; char str[100]; fscanf(di, "%s", partname); while( !feof(di)) { FILE *out; out = fopen(partname, "rb"); if(out == NULL) { printf("Open %s file failed\n", partname); exit(0); } while(1) { int r = fread(str, sizeof(char), 100, out); if(r<1) break; fwrite(str, sizeof(char), r, fp); } fclose(out); fscanf(di, "%s", partname); } printf("Congratulate you've merge the file!\n"); }

    再一次执笔修改

    #include <stdio.h> #include <stdlib.h> #include <string.h> int Choice(); void Divide(FILE *fp, int size, FILE *div); void Merge(FILE *fp, FILE *di); int main(int argc, char *argv[]) { int m = Choice(); if(m == 1) { if(argc != 4) { printf("Para must be 3 numbers!\n"); exit(0); } FILE *fp, *div; fp = fopen(argv[1], "rb"); div = fopen(argv[3], "wb");//用于生成目录 if(fp == NULL || div == NULL) { printf("open file failed!\n"); exit(0); } int size = atoi(argv[2]);//指定分割文件的大小 Divide(fp, size, div); fclose(div); fclose(fp); } else { FILE *fp, *di, *mod; fp = fopen(argv[1], "wb");//合并后的文件名 mod = fopen(argv[2], "rb");//实现排序 char s[10000][20]; char str[20]; int a[10000]; int k=0; fscanf(mod, "%s", s[0]); while( !feof(mod)) { sscanf(s[k], "%[^.]", str); a[k] = atoi(str); fscanf(mod, "%s", s[++k]); } int n=k; for(int i=0;i<n-1;i++) { for(int j=i+1;j<n;j++) { if(a[i] > a[j]) { int t; t = a[i]; a[i] = a[j]; a[j] = t; } } } fclose(mod); mod = fopen(argv[2], "wb"); for(int k=0; k<n; k++) fprintf(mod, "%d\r\n", a[k]); fclose(mod); mod = fopen(argv[2], "wb"); char part[1000]; for(k=0; k<n; k++) { itoa(a[k], part, 10); strcat(part, ".part"); fprintf(mod, "%s\r\n", part); } fclose(mod); di = fopen(argv[2], "rb");//目录 if(fp == NULL || di == NULL) { printf("Open file failed!\n"); exit(0); } Merge(fp, di); fclose(di); fclose(fp); } return 0; } int Choice() { int m; printf("Welcome come to the V2.0 FILE OPERATE machine!\n"); printf("\n---------------------------------------------\n"); printf("Please input your choice!\n\n"); printf("\t 1.The divide operate!\t \n"); printf("\t 2.The merge operate!\t \n"); while(1) { scanf("%d", &m); if(m!=1 && m!=2) { printf("Please input legal data!\n"); continue; } break; } return m; } void Divide(FILE *fp, int size, FILE *div) { char partname[100]; char ch; int flag = 0; for(int a=1; ;a++) { itoa(a, partname, 10); strcat(partname, ".part"); FILE *out; out = fopen(partname, "wb"); if(out == NULL) { printf("Open file failed\n"); exit(0); } for(int i=1; i<=size; i++) { fread(&ch, sizeof(char), 1, fp); if(feof(fp)) { flag = 1; break; } fwrite(&ch, sizeof(char), 1, out); } fprintf(div,"%s\r\n", partname); fclose(out); if(flag == 1) break; } printf("Congratulate you've divide the file!\n"); } void Merge(FILE *fp, FILE *di) { char partname[100]; char str[100]; fscanf(di, "%s", partname); while( !feof(di)) { FILE *out; out = fopen(partname, "rb"); if(out == NULL) { printf("Open %s file failed\n", partname); exit(0); } while(1) { int r = fread(str, sizeof(char), 100, out); if(r<1) break; fwrite(str, sizeof(char), r, fp); } fclose(out); fscanf(di, "%s", partname); } printf("Congratulate you've merge the file!\n"); } ReCclay 认证博客专家 嵌入式软件开发 机器/深度学习 全栈技术学习者 大家好,我是博主ReCclay,目前处于研究生阶段,就读于电子科技大学,主攻方向为汽车辅助驾驶算法研究。入站以来,凭借坚持与热爱,以博文的方式分享所学,截止目前累计博文数量达800余篇,累计受益人次达130w+次,涉及领域包括但不限于物联网开发、单片机开发、Linux驱动开发、FPGA开发、前/后端软件开发等。在未来我将继续专注于嵌入式相关领域,学习更多的科技知识,输出更高质量的博文。
    转载请注明原文地址: https://ju.6miu.com/read-1219.html

    最新回复(0)