S
sachin bond
The following code(in c++) is supposed to divide a file into 'n' different
files.
suppose i'm having a file called "input.zip".
The execution of the following code should divide "input.zip" into 'n'(user
specified) different files.
However the code currently..though makes 'n' different files... the divided
contents are stored only in the first of the 'n' files.
That is.. if "input.zip" has 25kb size, and if i want to divide it into 5
different files... then,
actually i should have 5 files with 5 kb each. but this code only gives me 1
file with 5 kb and the rest 4 files are all 0kb.
void breaker()
{
long int temppos = 0,pos = 0;
int n = 0;
char choice =' ';
char file[20] = "input.zip",filename[2][30];
char byte[1] = " " ;
strcpy(filename[0],file);
strcpy(filename[1],filename[0]);
strcat(filename[1],"0");
ifstream infile(filename[0],ios::binary);
infile.seekg(0,ios::end);
pos=infile.tellg();
do
{
cout<<" file size in kb : "<<pos/1000;
cout<<"\nenter number files to be broken into: ";
cin>>n;
temppos=pos/n;
cout<<temppos<<"\n do you want to continue?..press q to re-enter number of
files..or any other key to continue..";
choice = getch();
}while(choice=='q');
int ctr=0;
ofstream outfile(filename[1],ios::binary);
infile.seekg(0,ios::beg);
char str[1];
infile.read(str,1);
char ext[1]="0";
for(int i=0;i<n;i++)
{
ctr=0;
while(ctr<temppos)
{
outfile.write(str,1);
infile.read(str,1);
ctr++;
}
ext[0]++;
outfile.close();
strcpy(filename[1],filename[0]);
strcat(filename[1],ext);
ofstream outfile(filename[1],ios::binary);
}
outfile.close();
infile.close();
}
files.
suppose i'm having a file called "input.zip".
The execution of the following code should divide "input.zip" into 'n'(user
specified) different files.
However the code currently..though makes 'n' different files... the divided
contents are stored only in the first of the 'n' files.
That is.. if "input.zip" has 25kb size, and if i want to divide it into 5
different files... then,
actually i should have 5 files with 5 kb each. but this code only gives me 1
file with 5 kb and the rest 4 files are all 0kb.
void breaker()
{
long int temppos = 0,pos = 0;
int n = 0;
char choice =' ';
char file[20] = "input.zip",filename[2][30];
char byte[1] = " " ;
strcpy(filename[0],file);
strcpy(filename[1],filename[0]);
strcat(filename[1],"0");
ifstream infile(filename[0],ios::binary);
infile.seekg(0,ios::end);
pos=infile.tellg();
do
{
cout<<" file size in kb : "<<pos/1000;
cout<<"\nenter number files to be broken into: ";
cin>>n;
temppos=pos/n;
cout<<temppos<<"\n do you want to continue?..press q to re-enter number of
files..or any other key to continue..";
choice = getch();
}while(choice=='q');
int ctr=0;
ofstream outfile(filename[1],ios::binary);
infile.seekg(0,ios::beg);
char str[1];
infile.read(str,1);
char ext[1]="0";
for(int i=0;i<n;i++)
{
ctr=0;
while(ctr<temppos)
{
outfile.write(str,1);
infile.read(str,1);
ctr++;
}
ext[0]++;
outfile.close();
strcpy(filename[1],filename[0]);
strcat(filename[1],ext);
ofstream outfile(filename[1],ios::binary);
}
outfile.close();
infile.close();
}