Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   File Splitter and Merger (http://www.go4expert.com/forums/file-splitter-merger-t1267/)

Jaihind 26Aug2006 12:42

File Splitter and Merger
 
File Splitter is a very useful utility application for downloading . User want to download and store the downloaded files into Floppy disk , Its possible only if the downloaded file size is Less than or equal to 1.38 MB , If file size is more than the floppy disk size then File Splitter is required.

File Splitter and Merger performs the following tasks

1) Read the Original File and Find its File Size
2) Creates new files for writing depending on target file size splits needed
3) Allocate memory for newly created files depending on file size
4) Read original file information and write their content to newly created files
5) Stores splited file content into floppy disk
6) Finally join the splited file into original file
( Read splited files and write splited file information into a single file )

File Splitter and Merger Consist of the Following Modules
a) Auto Splitter
b) Custom Splitter
c) Merger

Auto Splitter
Auto file spitter splits the file automatically according to the source file size.

Suppose , If the user Read Red.txt file

The System Reads File Size and Display the file size : 3 MB the original file( Red.txt )

Splitted into Three Pieces of files :
Automatically creates three file names ( Red1.txt, Red2.txt and Red3.txt). And divides original file size by three. Resultant file size to allocate dynamically three pieces of files.

File Name :Red1.txt File Size1 :1 MB
File Name :Red2.txt File Size2 :1 MB
File Name :Red3.txt File Size3 :1 MB

After Splitting the file the merger merges the above files into original file format.

Custom Splitter
Custom splitter splits the files depending upon the customer requirements. If the users want three pieces of files depending upon the file size. The Original File Split into 3 Pieces of files. Other procedures same as the above Auto Splitter after splitting the file the merger merges the above files into original file.

Merger
Merger merges the splitted file into original single file. Merger reads splitted files information and writes into single original file.

Sample Code:

Auto File Splitter
Code: CPP

void ReadAuto(){
    cout<<"\nEnter the name of the file "<<endl;
    cin>>filename;
    Access(filename);
    _stat(filename,&buf);
    size=buf.st_size;
    cout<<"\n\nTotal File Size : : "<<size<<endl;
    autosplit(filename,size,piece);
}
// Auto Splitter

void autosplit(const char *filename,unsigned int size1,int piece)
{
    size1=size/piece;
    remainder=size%piece;
    filepiecesize=(char **)malloc(sizeof(char)*piece);
   
    for(i=1;i<=piece;i++)
    {
        if(i<piece)
            filepiecesize[i]=(char *)malloc(size1);
        else
            filepiecesize[i]=(char *)malloc(size1+remainder);
    }
    for(i=1;i<=piece;i++)
    {
        if(i<piece)    cout<<"\nFirst : \n"<<size1<<endl;
        else
            cout<<"\nSecond : "<<size1+remainder<<endl;
    }
   
    //OPEN THE FILE FOR READING
   
    fp=fopen(filename,"r");
    fread(buffer1,size,1,fp);
   
    for(i=1;i<=piece;i++)
    {
        cout<<"\nEnter the new file for writing"<<endl;
        cin>>filename1;
       
        if(i < piece)
        {
            if(i<piece)
            {
                fp2=fopen(filename1,"w");
                fread(buffer1,size1,1,fp);
                fwrite(buffer1,size1,1,fp2);
            }
            else
            {
                fp3=fopen(filename1,"w");
                fread(buffer2,size1+remainder,1,fp);
                fwrite(buffer2,size1+remainder,1,fp3);
            }
        }
    }
    fcloseall();
}

Custom Splitter
Code: CPP

void ReadCustomSplit()
{
    cout<<"\nEnter the name of the file "<<endl;
    cin>>filename;
    _stat(filename,&buf);
    size=buf.st_size;
    Access(filename);
    memory=size;
    CustomSplit(filename,memory);
}


#define MAXPIECESIZE 3

void CustomSplit(const char *filename,unsigned  int size)
{
    cout<<"\nHow Many Pieces Do you Want ?"<<endl;
    cin>>nofpiece);
    if(MAXPIECESIZE<=3)
    {
        cout<<"\n\nTotal Size of the File  : "<<size<<endl;
        size1=size/nofpiece;
        remainder=size%nofpiece;
        filepiecesize=(char **)malloc(sizeof(char)*nofpiece);
        for(i=1;i<=nofpiece;i++)
        {
            if(i<nofpiece)
                filepiecesize[i]=(char *)malloc(size1);
            else
                filepiecesize[i]=(char *)malloc(size1+remainder);
        }
        for(i=1;i<=nofpiece;i++)
        {
            if(i<nofpiece)
                cout<<"\nFirst : \n"<<size1<<endl;
            else
                cout<<"\nSecond : \n"<<size1+remainder<<endl;
        }
        //OPEN THE FILE FOR READING
        fp=fopen(filename,"r");
       
        //fread(buffer1,size,1,fp);
       
        for(i=1;i<=nofpiece;i++)
        {
            if(i < nofpiece)
            {
                //fp=fopen(filename,"r");
                n=atoi(filename)// Convert Array To Integer
                strcpy(string,filename);
                p+=n+1;
                itoa(p,str,10)// Convert Integer To Array
                strcat(filename,str);
                cout<<"New Name    : \n\n"<<i<<"\t"<<filename<<endl;
                fp1=fopen(filename,"w");
                fread(buffer1,size1,1,fp);
                //ptr=(char *)memcpy(buffer4,buffer1,strlen(buffer1));
                //if(ptr)
                //{
                //memmove(buffer4,buffer1,size1);
                fwrite(buffer4,size1,1,fp1);
                cout<<"\nmain : %u\n"<<size1<<endl;
               
                strcpy(filename,string);
            }
            else
            {
                n=atoi(filename)// Convert Array To Integer
                strcpy(string,filename);
                p+=n+1;
                itoa(p,str,10)// Convert Integer To Array
                strcat(filename,str);
                cout<<("\nNew Name : "<<I<<"\t"<<filename<<endl;
                fp2=fopen(filename,"w");
                fread(buffer2,size1+remainder,1,fp);
                cout<<"\nRemain : "<<size1+remainder<<endl;
                //ptr=(char *)memcpy(buffer3,buffer2,strlen(buffer2));
                //if(ptr)
                //memmove(buffer3,buffer2,size1+remainder);
                fwrite(buffer3,size1+remainder,1,fp2);
                strcpy(filename,string);
            }
        }
    }
    else
    {   
        perror(" Check the number of pieces : ");
        perror("Piece Not Greater than three(3) : ");
    }
    fcloseall();
    getch();
}

// File Accessibility

// Check Whether File Exists or Not

void Access(const char *filename)
{
    if(_access(filename,0)!=-1)
        cout<<"\nFile Exist "<<endl;
    else
    {
        ErrorChecking(error);
        exit(1);
    }
}



All times are GMT +5.5. The time now is 23:54.