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. - #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;
-
}
2 1442
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++.
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 :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |