日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C/C++简单实现文件分块

發布時間:2023/12/29 c/c++ 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C/C++简单实现文件分块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C語言簡單實現文件分塊

模塊1:分割文件
指定目標輸入文件(文件名或文件路徑)和分割尺寸,要求分割尺寸(單位:MB)為正整數,且范圍在[MIN_SIZE, MAX_SIZE]。
分割后產生塊文件,命名格式為“part_”+編號。

模塊2:合并文件
指定目標輸出文件(文件名或文件路徑)。
順序合并塊文件。

#include<stdio.h> #include<stdlib.h>#define NAME_LENGTH 100 #define BUFFER_SIZE 1024 #define MIN_SIZE 1 #define MAX_SIZE 1024void FileSplit(FILE *file,int size){if(file==NULL){printf("Unable to read file.\n");exit(-1);}if(size<MIN_SIZE || size>MAX_SIZE){printf("The size must be a positive integer(%dMB to %dMB).\n",MIN_SIZE,MAX_SIZE);exit(-3);}char partname[NAME_LENGTH];int buffer[BUFFER_SIZE]; int num=0;while(!feof(file)){sprintf(partname,"part_%d",++num);FILE *fout=fopen(partname,"wb");if(file==NULL){printf("Unable to create file.\n");exit(-2);}for(int i=0;i<size && !feof(file);i++){//read size * 1MBfor(int j=0;j<1024 && !feof(file);j++){//read 1MBint cnt=fread(buffer,1,1024,file);//read 1KB, may less than 1KB.fwrite(buffer,1,cnt,fout);}}fclose(fout);} }void FileMerge(char *filename){char partname[NAME_LENGTH];int buffer[BUFFER_SIZE]; int num=0;FILE *fin,*fout=fopen(filename,"wb");if(fout==NULL){printf("Unable to create file.\n");exit(-2);} while(sprintf(partname,"part_%d",++num) && (fin=fopen(partname,"rb"))!=NULL){while(!feof(fin)){int cnt=fread(buffer,1,1024,fin);//1KBfwrite(buffer,1,cnt,fout);}fclose(fin);}fclose(fout); }int main(){printf("Selection function:\n1.FileSplit\n2.FileMarge\n0.quit\n>>");int choose=0;while(scanf("%d",&choose) && choose){while(getchar()!='\n') continue;char filename[NAME_LENGTH];if(choose==1){printf("Please enter the name of the target file>>\n");gets(filename); FILE *fin=fopen(filename,"rb");printf("Please enter the size(MB) of each block>>\n");int size=0;scanf("%d",&size);printf("Wait...\n");FileSplit(fin,size);fclose(fin);printf("Finish.\n");}else if(choose==2){printf("Please enter the name of the output file>>\n");gets(filename); printf("Wait...\n");FileMerge(filename);printf("Finish.\n");}printf("Continue selecting function\n>>");}return 0; }

建議:檢驗原文件和合并文件的MD5值是否相同。

總結

以上是生活随笔為你收集整理的C/C++简单实现文件分块的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。