473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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();
}
Jul 19 '05 #1
8 2377


sachin bond wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #2
fl***********@hotmail.com (sachin bond) wrote in message news:<bj***********@hotmail.com>...
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
Jul 19 '05 #3
sachin bond wrote:
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

Jul 19 '05 #4
Karl Heinz Buchegger wrote:

sachin bond wrote:

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
Jul 19 '05 #5
Default User wrote:
[SNIP]

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

--
WW aka Attila
Jul 19 '05 #6

White Wolf wrote:

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. ;-)
Jul 19 '05 #7
Alexander Terekhov wrote:
White Wolf wrote:

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

--
WW aka Attila
Jul 19 '05 #8

White Wolf wrote:
[...]
Belij...
sneg (snow).
what is the wolf in Russian. Cabaka is dog...


Volchara is wolf.

regards,
alexander.
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Hari Om | last post by:
Here are the details of my error log files: I execute the command and get following message at console: ---------------------------------------------------------------------- ../sqlldr...
6
by: Uttam | last post by:
Hello, I am using the code from Chapter 17 specifically the code from the font frmListFonts in my application. I have taken care to copy all the relevant modules / class modules into the...
1
by: enrio | last post by:
I have made a number of changes to the code of an access 2000 application, and now the person responsible for the application wants to import one change at a time into his "master copy", after he...
46
by: Profetas | last post by:
Hi, I know that this is off topic. but I didn't know where to post. Do you comment your source code while coding or after coding. for example: you write a procedure and after it is...
22
by: Tommy | last post by:
Hi all. I am studying computer security, and I got this short and simple (?) c-code. Something is logical wrong in this code, and if used in the wrong hands of someone, it could be taken advantage...
0
by: Roald | last post by:
I am working on an application that needs to implement logical deletes instead of removing the records permanently. The logical delete works by setting 3 fields (deleted flag, date and user). I...
14
by: blueboy | last post by:
Hi, I am planning to automate a nighty restore of a DB on another server can someone point me in the right direction with the SQL script to modify the logical file names to the correct path and...
2
by: gnanapoongothai | last post by:
what is logical error found in the code below .Could any find. If( (x!=3) || (x!=4) ) printf("something"); else printf("something") thanks in advance for your answers.
3
by: anchitgood | last post by:
I have tried a lot but unable to correct just 1 logical error in the following code. This error is in the for loop, when fine and minutes are printed as the garbage values. Please check it out: ...
7
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.