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

  • Thread starter Karl Heinz Buchegger
  • Start date
K

Karl Heinz Buchegger

sachin said:
strcpy(filename[1],filename[0]);
strcat(filename[1],&ext);

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.
outfile.open(filename[1],ios::binary);//this line was the culprit..

It's also a good idea, to check, if the open succeeded.
}


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

}

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.
 
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.



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


}
 
C

Chris Dams

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';

should be char ext[]="1"; see below why.
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);

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



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


}

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
 
?

=?ISO-8859-1?Q?Christian_Brechb=FChler?=

sachin said:
The following code(in c++) is supposed to divide a file into 'n' different
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.

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,
char ext='1';


for(int i=0;i<n;i++)
{
ext++; [...]
}

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
 
D

Default User

Karl said:
sachin said:
strcpy(filename[1],filename[0]);
strcat(filename[1],&ext);

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


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
 
A

Alexander Terekhov

White said:
Default User wrote:
[SNIP]

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

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. ;-)
 
W

White Wolf

Alexander said:
White said:
Default User wrote:
[SNIP]

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

Yeah. And they also need to be humans, not animals, Wolf.

Actually I am a Jawa. :)
(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).

I would rather be black. My skin is killing me.
P.S. Yeah, I know, Feher, Farkas, your mother, and so forth. ;-)

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... :)
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top