473,473 Members | 3,363 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

spliting and recomposing files

10 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5.  
  6. int Spliter(char filename[250])
  7. {
  8.     int lastsize=0,bitnum=0,buffersize=0,j=0,totalsize=0, lastbuffer=0;
  9.     char *buffer,name[80],c1[80];
  10.     string c;
  11.  
  12.     ifstream inpt;
  13.     ofstream outpt;
  14.  
  15.     inpt.open(filename,ios::ate);
  16.     bitnum=inpt.tellg();  // gets bitnum (filesize)
  17.     inpt.close();
  18.  
  19.     cout<<"Size Of File:"<<bitnum<<"Bytes"<<endl;
  20.     buffersize=bitnum/10;         // calculates size of buffers needed
  21.     lastbuffer=bitnum%10;         // last buffer size
  22.     cout<<"Last buffer:"<<lastbuffer<<endl;
  23.  
  24.     buffer=new char[buffersize];
  25.  
  26.     inpt.open(filename,ios::binary);
  27.  
  28.     for(int i=0;i<9;i++)
  29.     {
  30.         inpt.read(buffer,buffersize);     // reads part of file into memory
  31.  
  32.         strcpy(name,"test");               //
  33.         itoa(i,c1,10);                // names files where data will be stored
  34.         strcat(name,c1);                   //
  35.         strcat(name,".jpg");               //    
  36.         cout<<"Le nom est:"<<name<<endl;
  37.  
  38.         outpt.open(name,ios::binary);
  39.                 //open the new created file to put the data in
  40.  
  41.         outpt.write(buffer,buffersize);      
  42.                  //writes the data into the file from memory
  43.         cout<<"\nBuffer is:"<<buffer<<endl<<endl
  44.             <<"Actual Buffersize:"<<strlen(buffer)<<endl;
  45.  
  46.         outpt.close();
  47.     }
  48.  
  49.     buffersize+=lastbuffer;                  
  50.     buffer=new char[buffersize];
  51.     strcpy(name,"test");     //same as last loop but exception handeling
  52.     inpt.read(buffer,buffersize);      //for last buffer
  53.     itoa(i,c1,10);
  54.     strcat(name,c1);
  55.     strcat(name,".jpg");
  56.     cout<<"Le nom est:"<<name<<endl;
  57.  
  58.     outpt.open(name,ios::binary);
  59.  
  60.     outpt.write(buffer,buffersize);
  61.  
  62.     outpt.close();        
  63.     inpt.close();
  64.  
  65.     return bitnum;
  66.  
  67. }
  68.  
  69. void Recomposer(char *filename,int bitnum)
  70. {
  71.     char name[80],c1[80],*buffer;
  72.     int buffersize,lastbuffer;
  73.     ifstream inpt;
  74.     ofstream outpt;
  75.  
  76.     buffersize=bitnum/10;    // calculates size of buffers needed
  77.     lastbuffer=bitnum%10;    // last buffer size          
  78.     buffer=new char[buffersize];
  79.  
  80.     outpt.open("DupliTest.jpg",ios::binary);   
  81.         //creates new file to recompose all the other files into
  82.  
  83.     for(int i=0;i<9;i++)
  84.     {
  85.         strcpy(name,"test");
  86.         itoa(i,c1,10);                    
  87.         strcat(name,c1);              //gets the name of the file chunks
  88.         strcat(name,".jpg");
  89.  
  90.         inpt.open(name,ios::binary);
  91.         inpt.read(buffer,buffersize);   
  92.                 //puts the data into the new file one chunk at a time
  93.         outpt.write(buffer,buffersize);
  94.     }
  95.  
  96.     buffersize+=lastbuffer;
  97.     buffer=new char[buffersize];  
  98.     strcpy(name,"test");             //same as last loop but exception handeling
  99.     itoa(i,c1,10);                     //for last buffer
  100.     strcat(name,c1);
  101.     strcat(name,".jpg");
  102.     inpt.open(name,ios::binary);
  103.     inpt.read(buffer,buffersize);
  104.     outpt.write(buffer,buffersize);
  105.  
  106.     outpt.close();
  107.  
  108.  
  109. }
  110.  
  111. int main()
  112. {
  113.     char filename[250];
  114.     int filesize=0;
  115.  
  116.     cin.getline(filename,250);
  117.     filesize=Spliter(filename);
  118.     Recomposer(filename,filesize);
  119.     return 0;
  120. }
May 12 '08 #1
2 1442
weaknessforcats
9,208 Recognized Expert Moderator Expert
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++.
May 12 '08 #2
Draggonn
10 New Member
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 :)
May 13 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Mike | last post by:
I am sure that I am making a simple boneheaded mistake and I would appreciate your help in spotting in. I have just installed apache_2.0.53-win32-x86-no_ssl.exe php-5.0.3-Win32.zip...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
0
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug...
18
by: JKop | last post by:
Here's what I know so far: You have a C++ project. You have source files in it. When you go to compile it, first thing the preprocessor sticks the header files into each source file. So now...
9
by: Sreejith S S Nair | last post by:
hi there, I have a panel control which contain more than 10 label controls. these label controls are added dynamically by user. In this application user can slit a control into not less than...
0
by: Sreejith S S Nair | last post by:
hi, Setp one. i have one panel control in my form. I am spliting this panel control into two part say working region and holding region. This spliting is only logic spliting. That is i...
2
by: seanf67 | last post by:
vbscript: Spilt Issue My text files are delimited as follows: Record Delimiter: vbCRLF Field Delimiter: ~ I need to break each record apart based on the first field which defines the...
3
by: aRTx | last post by:
I have try a couple of time but does not work for me My files everytime are sortet by NAME. I want to Sort my files by Date-desc. Can anyone help me to do it? The Script <? /* ORIGJINALI
1
by: kalia | last post by:
I have a text file and i want to Split the file into mulitple files based off the city and then create new files with the city name. I am able to read the file and also chnaged the semi colon to a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.