Add to Favorites    Make Home Page 1291 Online  
 Language Categories  
 Our Services  

Home » C Home » File Operations Home » File Splitting & Merging Utility

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title File Splitting & Merging Utility
Author Deepak Gupta
Author Email dgupta_2000 [at] rediffmail.com
Description Its a File Splitting & Merging Utility in 'C'.

Category C » File Operations
Hits 15119
Code Select and Copy the Code
Code: #include<stdio.h> #include<conio.h> #include<sys/stat.h> // Funtion prototype to split in Equal size void split_equal(char *fname,long int pieces); // Funtion prototype to split in size defined by the user void split_size(char *fname, long int sp_size); // Funtion prototype to merge the splitted file void merge(char *o_fname, long int no_file); /*Main Starts*/ void main() { int ch; long int size,pieces,sp_size,no_file; char *fname,*o_fname; FILE *fp; struct stat statbuff; // stat structure to get file size clrscr(); printf(" SPLITTING PROCESS......"); printf(" Enter the path of the file to be splitted : "); fflush(stdin); gets(fname); // Name of the File to be splitted fp=fopen(fname,"rb+"); if(fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } stat(fname,&statbuff); size=statbuff.st_size; // Get the size of the file to splitted fclose(fp); printf(" The Size of the File is %ld bytes.",size); printf(" SELECT THE WAY IN WHICH TO SPLIT THE FILE : "); printf(" 1.) In Equal Numbers."); printf(" 2.) User Defined Sizes."); printf(" Enter your choice : "); scanf("%d",&ch); // Start case switch(ch) { case 1: printf(" Enter the Number of Pieces : "); scanf("%ld",&pieces); // Define the number of pieces in which file will be splitted if(pieces>size) { printf(" Invalid Number of pieces."); getch(); exit(0); } split_equal(fname,pieces); // Function call to split printf(" Files Successfully Splitted."); getch(); break; case 2: printf(" Enter the Size in which to split the files : "); scanf("%ld",&sp_size); // Define the size in which file will be splitted if(sp_size>size) { printf(" Invalid size of the pieces."); getch(); exit(0); } split_size(fname,sp_size); // Function call to split printf(" Files Successfully Splitted."); getch(); break; default: printf(" Invalid Choice."); getch(); exit(0); } // End Case printf(" A .bat file has been created, copy all the pieces "); printf(" and the .bat file to the desired destination and run "); printf(" the .bat file to get the merged file."); getch(); //The Merging Process /* printf(" Do You Want to Merge the Splitted Files (Y/N): "); fflush(stdin); ch=getchar(); if(toupper(ch)=='Y') { // Start The Merging Process clrscr(); printf(" MERGING PROCESS......."); printf(" Enter the Path and Name of the Merged Output File : "); fflush(stdin); gets(o_fname); // Get the name of Output file printf(" Enter the Number of Files which are to be Merged : "); scanf("%ld",&no_file); // Get the Number of splitted files to be merged merge(o_fname,no_file); // Function call for Merging the Files printf(" The Splitted Files have been successfully Merged."); getch(); } */ } // End Main // Start of function definition split_equal void split_equal(char *fname,long int pieces) { FILE *fp,*n_fp,*fp_bat; char ch,*p,q[50],t[50],z[50]; int i,j,k,x; char *n_fname,base[5]; long int size,f_size,offset; struct stat statbuff; stat(fname,&statbuff); size=statbuff.st_size; offset=size%pieces; f_size=(size-offset)/pieces; p=fname; printf(" The first %ld files will be of size %ld bytes each.",pieces-1,f_size); printf(" The %ldth file will be of size %ld bytes. ",pieces,f_size+offset); fp=fopen(fname,"rb+"); if(fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } strrev(p); for(j=0;p[j]!='\';j++) //Extracting the Name of file in variable q { q[j]=p[j]; } q[j]='<=pieces;i++) { itoa(i,base,10); strcat(base,q); strcpy(n_fname,t); strcat(n_fname,base); printf(" The File Name is : "); puts(n_fname); n_fp=fopen(n_fname,"wb+"); if(n_fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } // Writing the data in the bat file if(i<pieces) { fputs(base,fp_bat); fputs("+",fp_bat); } else { fputs(base,fp_bat); fputs(" ",fp_bat); } // Writing data for bat file ends here // Writing data in the splitted pieces if(i!=pieces) { while(ftell(n_fp)<f_size) { ch=fgetc(fp); fputc(ch,n_fp); } fclose(n_fp); } else { while(ftell(n_fp)<f_size+offset) { ch=fgetc(fp); fputc(ch,n_fp); } fclose(n_fp); } // Writing data in the splitted pieces ends here } fclose(fp); fputs(q,fp_bat); // Writing the destination filename when merged fclose(fp_bat); } // Start Function spilt_size definition void split_size(char *fname, long int sp_size) { FILE *fp,*n_fp,*fp_bat; char ch,*p,q[50],t[50],z[50]; int i,j,k,x; char *n_fname,base[5]; long int size,offset,no_pieces; struct stat statbuff; stat(fname,&statbuff); size=statbuff.st_size; // Get the size of the file to be splitted offset=size%sp_size; // Calculating the size of the last file(if any) no_pieces=(size-offset)/sp_size; // Calculating the Number of pieces in which the file will be splitted p=fname; if(offset>0) { printf(" The Number of pieces of File is : %ld",no_pieces+1); printf(" The first %ld files will be of size %ld bytes each.",no_pieces,sp_size); printf(" The %ldth file will be of size %ld byte. ",no_pieces+1,offset); } else { printf(" The Number of pieces of File is %ld of %ld bytes each. ",no_pieces,sp_size); } fp=fopen(fname,"rb+"); // Opening the File to be splitted if(fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } strrev(p); for(j=0;p[j]!='\';j++) //Extracting the Name of file in variable q { q[j]=p[j]; } q[j]='<=no_pieces;i++) // Complete Process of splitting here onwards { itoa(i,base,10); strcat(base,q); strcpy(n_fname,t); strcat(n_fname,base); printf(" The File Name is : "); puts(n_fname); n_fp=fopen(n_fname,"wb+"); // Opening the new file if(n_fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } // Writing the data in the bat file if(i<no_pieces) { fputs(base,fp_bat); fputs("+",fp_bat); } else { fputs(base,fp_bat); fputs(" ",fp_bat); } // Writing data for bat file ends here while(ftell(n_fp)<sp_size) { ch=fgetc(fp); // Copying the content till fputc(ch,n_fp); // that size in the new piece } fclose(n_fp); } if(offset>0) // Checking for whether last file of { // different size will be generated or not itoa(i,base,10); // if yes, then generating that file strcat(base,q); strcpy(n_fname,t); strcat(n_fname,base); printf(" The File Name is : "); // process similiar to above puts(n_fname); n_fp=fopen(n_fname,"wb+"); if(n_fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } // Writing the data in the .bat file fputs("+",fp_bat); fputs(base,fp_bat); fputs(" ",fp_bat); //Writing data in the last of the splitted piece while(ftell(n_fp)<offset) { ch=fgetc(fp); fputc(ch,n_fp); } //Writing data in the last of the splitted piece ends here fclose(n_fp); } fputs(q,fp_bat); // Writing the destination filename when merged fclose(fp_bat); fclose(fp); } // End of function definition split_size // Start Function Merge void merge(char *o_fname, long int no_file) { FILE *fp,*o_fp; long int i,size,count; char *f_name; char ch; struct stat statbuff; fp=fopen(o_fname,"ab"); // Opening the file which will be merged if(fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } for(i=1;i<=no_file;i++) { printf(" Enter the name of the number %ld File to be Merged : ",i); fflush(stdin); gets(f_name); // Getting the name of the Files which are to be merged stat(f_name,&statbuff); // Getting the size of the file to be merged size=statbuff.st_size; count=0; o_fp=fopen(f_name,"rb+"); // Opening the Spiltted files to get them merged if(o_fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } while(count<size) { ch=fgetc(o_fp); // Copying the contents of splitted files in the output file fputc(ch,fp); count++; } fclose(o_fp); } fclose(fp); } // End of Function mergent i,size,count; char *f_name; char ch; struct stat statbuff; fp=fopen(o_fname,"ab"); // Opening the file which will be merged if(fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } for(i=1;i<=no_file;i++) { printf(" Enter the name of the number %ld File to be Merged : ",i); fflush(stdin); gets(f_name); // Getting the name of the Files which are to be merged stat(f_name,&statbuff); // Getting the size of the file to be merged size=statbuff.st_size; count=0; o_fp=fopen(f_name,"rb+"); // Opening the Spiltted files to get them merged if(o_fp==NULL) { printf(" "); fprintf(stderr,"Cannot Open the File."); getch(); exit(0); } while(count

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi դ ? ??
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions
Copyright ©2003-2017 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=925


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review