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

Discussion in 'C++' started by Karl Heinz Buchegger, Sep 12, 2003.

  1. 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.
    It's also a good idea, to check, if the open succeeded.
    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, Sep 12, 2003
    1. Advertisements

  2. Karl Heinz Buchegger

    sachin bond Guest

    The following code(in c++) is supposed to divide a file into 'n' different

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


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


    cout<<" file size in kb : "<<(float)pos/1024;
    cout<<"\nenter number files to be broken into: ";
    cout<<temppos<<"\n do you want to continue?..press q to re-enter number of
    files..or any other key to continue..";


    int ctr=0;

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


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


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

    outfile.write(&byte, sizeof byte);

    infile.read(&byte, sizeof byte);


    outfile.open(filename[1],ios::binary);//this line was the culprit..


    sachin bond, Sep 12, 2003
    1. Advertisements

  3. Karl Heinz Buchegger

    Chris Dams Guest

    should be char ext[]="1"; see below why.
    This expects to get a 0 terminated string. Above I made ext one so
    this now goes ok if you change &ext to ext.
    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);

    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
    Chris Dams, Sep 12, 2003
  4. 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,
    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

    =?ISO-8859-1?Q?Christian_Brechb=FChler?=, Sep 12, 2003
  5. Karl Heinz Buchegger

    Default User Guest

    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:


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

    string filename = "blah blah";

    filename += ext;

    Brian Rodenborn
    Default User, Sep 12, 2003
  6. Karl Heinz Buchegger

    White Wolf Guest

    Default User wrote:

    Hey! Users cannot post here, only programmers! ;-)
    White Wolf, Sep 12, 2003
  7. 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


    P.S. Yeah, I know, Feher, Farkas, your mother, and so forth. ;-)
    Alexander Terekhov, Sep 12, 2003
  8. Karl Heinz Buchegger

    White Wolf Guest

    Actually I am a Jawa. :)
    I would rather be black. My skin is killing me.
    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... :)
    White Wolf, Sep 12, 2003
  9. White Wolf wrote:
    sneg (snow).
    Volchara is wolf.

    Alexander Terekhov, Sep 12, 2003
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.