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

Nuz25

Junior Member
Joined
Aug 20, 2010
Messages
129
Reaction score
100
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();
}



?????????????????????
 
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'?
 
Well actually im translating from french, and Im 100% sure that the word he uses in french means "the one before the last one"...
 
Back
Top