spliting and recomposing files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Draggonn
    New Member
    • Dec 2007
    • 10

    #1

    spliting and recomposing files

    Hello all. I'm not sure if this is the right part of the forum to ask, but I couldn't find any better place. I am trying to make a program that will take a file of any extension, and split it into several files, and then recompose these files into the original file. But for some reason, even though the copy file size appears correctly when the whole thing is done, I get an error when I open it. If it's an .exe i get an error and if its a picture I get drawing failed.. I've been trying for a long time now, but since I don't have much experience with binary files, I really can't find what I did wrong.
    Code:
    #include<iostream>
    #include<fstream>
    
    using namespace std;
    
    int Spliter(char filename[250])
    {
    	int lastsize=0,bitnum=0,buffersize=0,j=0,totalsize=0, lastbuffer=0;
    	char *buffer,name[80],c1[80];
    	string c;
    	
    	ifstream inpt;
    	ofstream outpt;
    
    	inpt.open(filename,ios::ate);
    	bitnum=inpt.tellg();  // gets bitnum (filesize)
    	inpt.close();
    
    	cout<<"Size Of File:"<<bitnum<<"Bytes"<<endl;
    	buffersize=bitnum/10;         // calculates size of buffers needed
    	lastbuffer=bitnum%10;         // last buffer size
    	cout<<"Last buffer:"<<lastbuffer<<endl;
    
    	buffer=new char[buffersize];
    
    	inpt.open(filename,ios::binary);
    		
    	for(int i=0;i<9;i++)
    	{
    		inpt.read(buffer,buffersize);     // reads part of file into memory
    		
    		strcpy(name,"test");               //
    		itoa(i,c1,10);	            // names files where data will be stored
    		strcat(name,c1);				   //
    		strcat(name,".jpg");			   //	
    		cout<<"Le nom est:"<<name<<endl;
    	
    		outpt.open(name,ios::binary);
                    //open the new created file to put the data in
    
    		outpt.write(buffer,buffersize);	  
                     //writes the data into the file from memory
    		cout<<"\nBuffer is:"<<buffer<<endl<<endl
    			<<"Actual Buffersize:"<<strlen(buffer)<<endl;
    
    		outpt.close();
    	}
    
    	buffersize+=lastbuffer;                  
    	buffer=new char[buffersize];
    	strcpy(name,"test");	 //same as last loop but exception handeling
    	inpt.read(buffer,buffersize);	  //for last buffer
    	itoa(i,c1,10);
    	strcat(name,c1);
    	strcat(name,".jpg");
    	cout<<"Le nom est:"<<name<<endl;
    	
    	outpt.open(name,ios::binary);
    	
    	outpt.write(buffer,buffersize);
    	
    	outpt.close();		
    	inpt.close();
    
    	return bitnum;
    
    }
    
    void Recomposer(char *filename,int bitnum)
    {
    	char name[80],c1[80],*buffer;
    	int buffersize,lastbuffer;
    	ifstream inpt;
    	ofstream outpt;
    
    	buffersize=bitnum/10;    // calculates size of buffers needed
    	lastbuffer=bitnum%10;    // last buffer size          
    	buffer=new char[buffersize];
    
    	outpt.open("DupliTest.jpg",ios::binary);   
            //creates new file to recompose all the other files into
    											   
    	for(int i=0;i<9;i++)
    	{
    		strcpy(name,"test");
    		itoa(i,c1,10);                    
    		strcat(name,c1);              //gets the name of the file chunks
    		strcat(name,".jpg");
    		
    		inpt.open(name,ios::binary);
    		inpt.read(buffer,buffersize);   
                    //puts the data into the new file one chunk at a time
    		outpt.write(buffer,buffersize);
    	}
    
    	buffersize+=lastbuffer;
    	buffer=new char[buffersize];  
    	strcpy(name,"test");             //same as last loop but exception handeling
    	itoa(i,c1,10);					 //for last buffer
    	strcat(name,c1);
    	strcat(name,".jpg");
    	inpt.open(name,ios::binary);
    	inpt.read(buffer,buffersize);
    	outpt.write(buffer,buffersize);
    
    	outpt.close();
    
    
    }
    
    int main()
    {
    	char filename[250];
    	int filesize=0;
    
    	cin.getline(filename,250);
    	filesize=Spliter(filename);
    	Recomposer(filename,filesize);
    	return 0;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Have you considered using iterators and the STL copy algorithm and avoid all this code?

    Also, I would not be using C library funcitons like strcpy, strcat as thay have all been deprecated in C++.

    Comment

    • Draggonn
      New Member
      • Dec 2007
      • 10

      #3
      Originally posted by weaknessforcats
      Have you considered using iterators and the STL copy algorithm and avoid all this code?

      Also, I would not be using C library funcitons like strcpy, strcat as thay have all been deprecated in C++.
      I still haven't taken anything related to classes in C++. I have only taken 2 programming courses so far, and I know only as far as linked lists, stacks and queues. Guess I should hold off doing such programs till I took classes and such.
      Anyways, thank you for your help :)

      Comment

      Working...