All platform filespliter C - source code

  • msurii
  • Topic Author
  • Offline
  • Premium Member
  • Premium Member
More
13 years 4 months ago #2532 by msurii
Universal file spliter C-source code was provided here.If you need the executable for varios platform you can get it from
www.megaupload.com/?d=VGXVK0JN

All platform filespliter C - source code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
char SFile[512];
int noofparts=0;
int deleteflag=0;
/*=====================================================*/
/* Author - msurii */
/* Date - 27 May 2005 */
/*=====================================================*/
int filesplit(){
char c[1];
char DFile[512]="1.tar.01";
FILE *f,*fd;
int i;
long int filesize,count;
if (strlen(SFile) ==0){
printf("\n Enter the Source File to Split :");
gets(SFile);}
if (strlen(SFile) ==0) {
printf("File name can not be Empty\n");
exit(-1);}
if (noofparts ==0){
printf("\n Enter the No of Parts to Split :");
scanf("%d",&noofparts);}
if ((f=fopen (SFile,"rb"))==NULL)
{
printf("\nError opening file = %s\n",SFile);
exit(-1);
}
fseek(f,0,SEEK_END);
filesize=ftell(f);
rewind(f);
printf("\nSource FileSize = %ld",filesize);
printf("\nSplitting File Name = %s",SFile);
for(i=1;i!=noofparts+1;i++)
{
sprintf(DFile,"%s.%d",SFile,i);
printf("\nSplitting Part %d to %s",i,DFile);
if ((fd=fopen (DFile,"wb"))==NULL)
{
printf("\nError opening file = %s\n",DFile);
exit(2);
}
count=0;
while (!feof(f))
{
/*c=fgetc(f);
fputc(c,fd);*/
fread(c,1,1,f);
fwrite(c,1,1,fd);
count++;
if (i!=noofparts){
if (count == (filesize/noofparts)) break;
}
}
fclose (fd);
}
fclose(f);
if (deleteflag==1) remove(SFile);
printf("\nSplitting Completed\n");
return(0);
}

int filejoin()
{
char c[1],choice;
char DFile[512]="1.tar.01";
FILE *f,*fd;
int i;
if (strlen(SFile) ==0){
printf("\n Enter the Source File to Join :");
gets(SFile);}
if (strlen(SFile) ==0) {
printf("\nFile name can not be Empty\n");
exit(-1);}
if ((f=fopen (SFile,"rb"))!=NULL)
{
printf("\nFile %s already exist",SFile);
printf("\nDo You Want To Overwrrite Y/N :");
scanf("%c",&choice);
if(choice!='Y' && choice!='y') exit(1);
}
if ((f=fopen (SFile,"wb"))==NULL)
{
printf("\nError opening file = %s\n",SFile);
exit(1);
}
printf("\nSource File Name = %s",SFile);
i=1;
while(1)
{
sprintf(DFile,"%s.%d",SFile,i);
printf("\nJoining Part %d - %s to %s",i,DFile,SFile);
if ((fd=fopen (DFile,"rb"))==NULL)
{
if (i==1) {printf("\nError opening file = %s\n",DFile);}
else {printf("\nJoin Completed\n");}
exit(2);
}
/*To avoid the last bit populating twice */
fread(c,1,1,fd);
while (!feof(fd))
{
/*c=fgetc(fd);
fputc(c,f);*/
fwrite(c,1,1,f);
fread(c,1,1,fd);
}
fclose (fd);
if (deleteflag==1) remove(DFile);
i++;
}
fclose(f);
return(0);
}


int string2int(char* digit) {
int result=0;
/*--- Convert each digit char and add into result.*/
while (*digit >= '0' && *digit <='9') {
result = (result * 10) + (*digit - '0');
digit++;
}

/*--- Check that there were no non-digits at end.*/
if (*digit != 0) {
return result;
}

return result;
}

int main(int argc, char **argv)
{
int choice;
char userchoice[40];
sprintf(SFile,"%s","");
if(argc>=3) sprintf(SFile,"%s",argv[2]);
if(argc>=4) noofparts=string2int(argv[3]);
if(argc>=5) deleteflag=string2int(argv[4]);
if(argc==1)
{
printf("Syntax filesplit S | J");
printf("\t\tS - Split| J - Join\n");
printf("\n\t\t1. SplitFile");
printf("\n\t\t2. JoinFile ");
printf("\n\t\tEnter Your Option");
scanf("%d",&choice);
if(choice!=1 && choice!=2) {
printf("\n\tInvalid option\n");
exit(-1);
}
}
else
{
choice=0;
sprintf(userchoice,"%s",argv[1]);
if (strcmp(userchoice,"S")==0) choice=1;
if (strcmp(userchoice,"J")==0) choice=2;
if (strcmp(userchoice,"s")==0) choice=1;
if (strcmp(userchoice,"j")==0) choice=2;
if (strcmp(userchoice,"-S")==0) choice=1;
if (strcmp(userchoice,"-J")==0) choice=2;
if (strcmp(userchoice,"-s")==0) choice=1;
if (strcmp(userchoice,"-j")==0) choice=2;

}
if (choice==0){
printf("\nInvalid options use S | J \n");
exit(-1);
}
fflush(stdin);
if (choice==1) filesplit();
if (choice==2) filejoin();
return(0);
}

Please Log in or Create an account to join the conversation.

Time to create page: 0.101 seconds