1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is It Me Or There's An Error In This??

Discussion in 'C, C++, C#' started by Nuz25, Dec 15, 2010.

  1. Nuz25

    Nuz25 Junior Member

    Joined:
    Aug 20, 2010
    Messages:
    129
    Likes Received:
    100
    Occupation:
    I'm a student
    Ok guys I have an exam (C/C++) tomorrow and I have this simple question that's pissing me off right now...

    " You have the binary file "INTEGERS.DAT" containing only integers. Write the declarations and instructions to open the file and affect the value zero to the penultimate integer of the file without rewriting the rest of the file. "


    Here's the given answer:


    int zero = 0;
    fstream file;
    file.open("INTEGERS.DAT", ios::binary|ios::in|ios:: out);

    if(!file.fail()){
    file.seekp(-1*sizeof(int),ios::end);
    file.write((char*)&zero, sizeof(int));
    file.close();
    }


    But shouldn't it be :


    int zero = 0;
    fstream file;
    file.open("INTEGERS.DAT", ios::binary|ios::in|ios:: out);

    if(!file.fail()){
    file.seekp(-2*sizeof(int),ios::end);
    file.write((char*)&zero, sizeof(int));
    file.close();
    }



    ?????????????????????
     
  2. MaDeuce

    MaDeuce Newbie

    Joined:
    Oct 24, 2008
    Messages:
    45
    Likes Received:
    16
    Location:
    Austin, TX
    I have to agree with you, but my C++ is getting rusty. Did you happen to compile it and run it to confirm your hypothesis? You could also run it from the debugger to better understand what's going on.

    I'm sure that your prof would be insulted, but is it possible he takes penultimate to mean 'last'?
     
  3. Nuz25

    Nuz25 Junior Member

    Joined:
    Aug 20, 2010
    Messages:
    129
    Likes Received:
    100
    Occupation:
    I'm a student
    Well actually im translating from french, and Im 100% sure that the word he uses in french means "the one before the last one"...