plz help me.. this logical error is irritating...

Discussion in 'C++' started by sachin bond, Sep 11, 2003.

  1. sachin bond

    sachin bond Guest

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


    }
     
    sachin bond, Sep 11, 2003
    #1
    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.