Connecting Tech Pros Worldwide Help | Site Map

i think this code has a logical error ..plz help me...

sachin bond
Guest
 
Posts: n/a
#1: Jul 19 '05
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.



please help me.. with this problem.



void breaker()
{


long int temppos = 0,pos = 0;
int n = 0;
char choice =' ';
char file[20] = "input.zip";
char filename[2][30];


strcpy(filename[0],file);
strcpy(filename[1],filename[0]);
strcat(filename[1],"1");

ifstream infile(filename[0],ios::binary);


infile.seekg(0,ios::end);
pos=infile.tellg();

do
{
cout<<" file size in kb : "<<(float)pos/1024;
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..";
cin.get(choice);

}while(choice=='q');

int ctr=0;


ofstream outfile(filename[1],ios::binary);

infile.seekg(0,ios::beg);

char byte;

infile.read(&byte,sizeof byte); //read and write functions used for working


on data the "binary" way.

char ext='1';


for(int i=0;i<n;i++)
{
ext++;



ctr=0;

while(ctr<temppos) //temppos = pos/n..in line 31
{


outfile.write(&byte, sizeof byte);


infile.read(&byte, sizeof byte);

ctr++;
}

outfile.close();
strcpy(filename[1],filename[0]);
strcat(filename[1],&ext);
outfile.open(filename[1],ios::binary);//this line was the culprit..
}



outfile.close();
infile.close();


}
Karl Heinz Buchegger
Guest
 
Posts: n/a
#2: Jul 19 '05

re: i think this code has a logical error ..plz help me...




sachin bond wrote:[color=blue]
>
> strcpy(filename[1],filename[0]);
> strcat(filename[1],&ext);[/color]

ext is a single character.
A single character is not a string!
Because a C-style string always has to be terminated by a '\0' character!

char Extension[2];
Extension[0] = ext;
Extension[1] = '\0';

strcat( filename[1], Extension );


Also: it's actually a good idea to output that filename during
the development process.
[color=blue]
> outfile.open(filename[1],ios::binary);//this line was the culprit..[/color]

It's also a good idea, to check, if the open succeeded.
[color=blue]
> }
>
>
> outfile.close();
> infile.close();
>
> }[/color]

As a rule of thumb:
File classes have some way to check if operations like open have
succeeded. It is an extremely good idea to use them! There is
no point in writing to a file stream which has not opened correctly,
ignoring the error checking facilities and yelling for help aftwards.

If the open failed, it is also a good idea to indicate which
file (filename) could not be opened or created. During the
development process it is also a good idea to output status
messages (eg: now opening file 'filename' - succeeded) which
can be removed once the program is finished.

It is also a good idea, if you program in C++, to use the C++
string class.

--
Karl Heinz Buchegger
kbuchegg@gascad.at
Chris Dams
Guest
 
Posts: n/a
#3: Jul 19 '05

re: i think this code has a logical error ..plz help me...


flirt4fun_143@hotmail.com (sachin bond) wrote in message news:<bjsedi$uo_002@hotmail.com>...[color=blue]
> 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.
>
>
>
> please help me.. with this problem.
>
>
>
> void breaker()
> {
>
>
> long int temppos = 0,pos = 0;
> int n = 0;
> char choice =' ';
> char file[20] = "input.zip";
> char filename[2][30];
>
>
> strcpy(filename[0],file);
> strcpy(filename[1],filename[0]);
> strcat(filename[1],"1");
>
> ifstream infile(filename[0],ios::binary);
>
>
> infile.seekg(0,ios::end);
> pos=infile.tellg();
>
> do
> {
> cout<<" file size in kb : "<<(float)pos/1024;
> 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..";
> cin.get(choice);
>
> }while(choice=='q');
>
> int ctr=0;
>
>
> ofstream outfile(filename[1],ios::binary);
>
> infile.seekg(0,ios::beg);
>
> char byte;
>
> infile.read(&byte,sizeof byte); //read and write functions used for working
>
>
> on data the "binary" way.
>
> char ext='1';[/color]

should be char ext[]="1"; see below why.
[color=blue]
>
> for(int i=0;i<n;i++)
> {
> ext++;
>
>
>
> ctr=0;
>
> while(ctr<temppos) //temppos = pos/n..in line 31
> {
>
>
> outfile.write(&byte, sizeof byte);
>
>
> infile.read(&byte, sizeof byte);
>
> ctr++;
> }
>
> outfile.close();
> strcpy(filename[1],filename[0]);
> strcat(filename[1],&ext);[/color]

This expects to get a 0 terminated string. Above I made ext one so
this now goes ok if you change &ext to ext.
[color=blue]
> outfile.open(filename[1],ios::binary);//this line was the culprit..
> }
>
>
>
> outfile.close();
> infile.close();
>
>
> }[/color]

Why do you open and close your outfile ousite the loop? You can do
something like the following pseudo-code

for(int i=0;i<numfilesiwanttocreate;++i)
{ openoutfilenumber(i);
writetothisfile();
closethefile();
}

This will spare you some lines of code, is much clearer and will not
create a useless extra empty file input.zip3 if you wanted to split
into two files. Alse why not use std::string?

Have a nice day,
Chris Dams
Christian Brechbühler
Guest
 
Posts: n/a
#4: Jul 19 '05

re: i think this code has a logical error ..plz help me...


sachin bond wrote:[color=blue]
> The following code(in c++) is supposed to divide a file into 'n' different
> files.[/color]
[...][color=blue]
> 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.[/color]

If you're interested in the result, use 'split -b5k' (a GNU utility,
most likely available in any Unix, and through cygwin on Windows).


If you're interested in the C++ exercise, heed the other's suggestions
-- use the string and iostream classes.

One more twist: you write,
[color=blue]
> char ext='1';
>
>
> for(int i=0;i<n;i++)
> {
> ext++;[/color]
[...][color=blue]
> }[/color]

You may be assuming that the successor of '1' is '2', and then '3'. If
you use ASCII, it is indeed. But C++ doesn't guarantee anything in
that respect. Apart from that, if n is large enough, you'll get a
character that's not acceptable in a filename on your operating system.

Good luck

Christian

Default User
Guest
 
Posts: n/a
#5: Jul 19 '05

re: i think this code has a logical error ..plz help me...


Karl Heinz Buchegger wrote:[color=blue]
>
> sachin bond wrote:[color=green]
> >
> > strcpy(filename[1],filename[0]);
> > strcat(filename[1],&ext);[/color]
>
> ext is a single character.
> A single character is not a string!
> Because a C-style string always has to be terminated by a '\0' character!
>
> char Extension[2];
> Extension[0] = ext;
> Extension[1] = '\0';
>
> strcat( filename[1], Extension );[/color]


I don't recommend creating a temp just to append a single char. If you
must use C-style strings, either find the end of the string and manually
insert the character (and a new terminator), or use the following:

strncat(filename[1],&ext,1);


Of course, the best in my opinion would be to use std::string:


string filename = "blah blah";

filename += ext;



Brian Rodenborn
White Wolf
Guest
 
Posts: n/a
#6: Jul 19 '05

re: i think this code has a logical error ..plz help me...


Default User wrote:
[SNIP]

Hey! Users cannot post here, only programmers! ;-)

--
WW aka Attila


Alexander Terekhov
Guest
 
Posts: n/a
#7: Jul 19 '05

re: i think this code has a logical error ..plz help me...



White Wolf wrote:[color=blue]
>
> Default User wrote:
> [SNIP]
>
> Hey! Users cannot post here, only programmers! ;-)[/color]

Yeah. And they also need to be humans, not animals, Wolf. (Well,
of course you just want to convey that you're "a man given to
paying unwanted sexual attention to women" and you're white, not
black).

regards,
alexander.

P.S. Yeah, I know, Feher, Farkas, your mother, and so forth. ;-)
White Wolf
Guest
 
Posts: n/a
#8: Jul 19 '05

re: i think this code has a logical error ..plz help me...


Alexander Terekhov wrote:[color=blue]
> White Wolf wrote:[color=green]
>>
>> Default User wrote:
>> [SNIP]
>>
>> Hey! Users cannot post here, only programmers! ;-)[/color]
>
> Yeah. And they also need to be humans, not animals, Wolf.[/color]

Actually I am a Jawa. :-)
[color=blue]
> (Well,
> of course you just want to convey that you're "a man given to
> paying unwanted sexual attention to women" and you're white, not
> black).[/color]

I would rather be black. My skin is killing me.
[color=blue]
> P.S. Yeah, I know, Feher, Farkas, your mother, and so forth. ;-)[/color]

Belij... what is the wolf in Russian. Cabaka is dog... Anyways I guess I
can apply now for the "most of topic" Guinness Award. Do they really give
Guinness? Glass or a mug... :-)

--
WW aka Attila


Alexander Terekhov
Guest
 
Posts: n/a
#9: Jul 19 '05

re: i think this code has a logical error ..plz help me...



White Wolf wrote:
[...][color=blue]
> Belij...[/color]

sneg (snow).
[color=blue]
> what is the wolf in Russian. Cabaka is dog...[/color]

Volchara is wolf.

regards,
alexander.
Closed Thread