#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
char nameinputt[80]; char namebefdot[76]; char nameaftdot[20]; char num[60][5]={ {"1"},{"2"},{"3"},{"4"},{"5"},{"6"}, {"7"},{"8"},{"9"},{"10"},{"11"},{"12"}, {"13"},{"14"},{"15"},{"16"},{"17"},{"18"}, {"19"},{"20"},{"21"},{"22"},{"23"},{"24"}, {"25"},{"26"},{"27"},{"28"},{"29"},{"30"}, {"31"},{"32"},{"33"},{"34"},{"35"},{"36"}, {"37"},{"38"},{"39"},{"40"},{"41"},{"42"}, {"43"},{"44"},{"45"},{"46"},{"47"},{"48"}, {"49"},{"50"},{"51"},{"52"},{"53"},{"54"}, {"55"},{"56"},{"57"},{"58"},{"59"},{"60"}}; char namenew[60][80]={{""}}; char namefile[61][80]={{""}}; char namedest[61][80];
void destname(int f) {int i,j,k; for(i=0;i<80;i++) {if(nameinputt[i]=='.') break; namebefdot[i]=nameinputt[i];} for(j=0,k=i;k<80&&j<20;k++,j++) {nameaftdot[j]=nameinputt[k];} strcat(namenew[f],namebefdot); strcat(namenew[f],num[f]); strcat(namenew[f],nameaftdot); }
int changename(int f) {f+=1; return f; }
long filelen(char* filename) { FILE *v_fpLog = NULL; long fSet = 0, fEnd = 0; long filelen = 0; if ( (v_fpLog = fopen( filename , "r+")) == NULL ) { printf( "The file '%s' was not opened", filename ); return 0; } else { fseek( v_fpLog, 0, SEEK_SET ); fSet = ftell( v_fpLog ); fseek( v_fpLog, 0, SEEK_END ); fEnd = ftell( v_fpLog ); /* file lenght */ filelen = (fEnd - fSet)/1024; } return filelen; }/*用于获得文件大小*/
void savename(int count) {FILE *fp ; int i,j;
if((fp = fopen("file", "w"))==NULL) {printf(" file文件不在此目录\n");exit(0);} for(j=0;j<80;j++) namefile[count][j]=nameinputt[j]; for(i=0 ; i <=count+1 ; i++) { fprintf(fp, "%s\n", namefile[i]) ; } fclose(fp) ; }
void savesize (long int size) { FILE *fp; if ((fp = fopen ("file2", "w")) == NULL) { printf (" file2文件不在此目录\n"); exit (0); } fprintf (fp, "%ld", size); fclose (fp); }/*保存分割文件大小,供合并时使用*/
int readname () { FILE *fp; int i = 0, j; int count; char *p;
if ((fp = fopen ("file", "r")) == NULL) { printf (" file文件不在此目录\n"); exit (0); } while (fgets (namedest[i], 80, fp) != NULL) { p = strchr (namedest[i], '\n'); if (p) *p = 0; i++; } for (j = 0, count = 0; j < 61; j++) { if (namedest[j][0] != '\0') count++; } fclose (fp); return count - 1; }/*合并时读取文件名信息*/
long int readsize () { FILE *fp; long int size; if ((fp = fopen ("file2", "r")) == NULL) { printf (" file2文件不在此目录\n"); exit (0); } fscanf (fp, "%ld", &size); fclose (fp); return size; }/*读取分割文件大小信息*/
int unite() { int i = 0; int count = 0, times,per; long int size = 0; FILE *creat, *file; printf (" 合并进行中\n"); printf (" 请稍后\n"); printf ("-------------------------------------------------------------------------------\n"); printf (" 已完成0%%\n"); count = readname (); size = readsize (); if ((creat = fopen (namedest[count], "wb")) == NULL) printf ("can't create file\n"); fclose (creat);
if ((creat = fopen (namedest[count], "ab")) == NULL) printf ("can't open created file\n"); do { if ((file = fopen (namedest[i], "rb")) == NULL) { printf ("can't open united file\n"); exit(0); } for(times=0; times<=size*1024 && (!feof(file)); times++) { fputc(fgetc(file),creat); } per = (i+1)/(float)count*100; printf (" 已完成%d%%\n",per); fclose (file); i++; } while (i < count); fclose (creat); printf (" 已成功,合并后的文件是:\n"); printf (namedest[count]); printf ("\n"); }
int main(int argc, char *argv[]) { FILE *in,*out; int i; int f = 0, k, times; long int size = 0,len; int per; if( (argc == 1) || (argv[1] == "--help") || (argv[1] == "/?") || ((argc == 2) && (argv[1][0] != 'u')) ) { printf("Usage: split \n"); printf("To unite: split u\n"); printf("Example: \" split file 3 \" -- split file to many parts,and every single part is 3KB's big\n"); exit(0); }
if( (argc == 2) && (argv[1][0] == 'u') ) { unite(); exit(0); } for(i=0;i<80;i++) {if(argv[1][i]!='\0') nameinputt[i]=argv[1][i]; else break; }
size = atoi(argv[2]);
if ((in = fopen (nameinputt, "rb")) == NULL) { printf ("-------------------------------------------------------------------------------\n"); printf (" 不能打开被分割文件,请检查输入的文件名\n"); printf ("-------------------------------------------------------------------------------\n"); exit(0); } len = filelen(nameinputt); printf ("-------------------------------------------------------------------------------\n"); printf ("\n 分割进行中,请耐心等待\n"); printf ("-------------------------------------------------------------------------------\n"); printf (" 已完成0%%\n"); printf (" 分割后的文件是\n"); while(!feof(in)) { destname(f); for (k = 0; k < 80; k++) namefile[f][k] = namenew[f][k]; printf (namenew[f]); printf ("\n"); if ((out = fopen (namenew[f], "wb")) == NULL) printf ("can't open out file\n"); for(times=0; times<=size*1024 && (!feof(in)); times++) { fputc(fgetc(in),out); } per = (f+1)/(float)(1+len/size)*100; printf (" 已完成%d%%\n",per); f = changename (f); fclose (out); } fclose (in); savename (f); savesize (size); printf (" 分割完毕\n"); printf ("-------------------------------------------------------------------------------\n"); }
|