code doesnt work......please help me..

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();


}
 
K

Kevin Goodsell

Josh said:
void breaker()
{
long int temppos = 0,pos = 0;
int n = 0;
char choice =' ';
char file[20] = "input.zip",filename[2][30];
char byte[1] = " " ;


Ouch! Buffer overflow already. The string " " has two characters: ' ' and
'\0'. You tried to store it in an array that can hold only one character.

No. It's a constraint violation that should not compile.


8.5.2 Character arrays [dcl.init.string]

2 There shall not be more initializers than there are array elements.
[Example:
char cv[4] = "asdf"; // error
is ill-formed since there is no space for the implied trailing '\0'.
]


-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top