Giorgi Aptsiauri
Giorgi Aptsiauri

Reputation: 355

fseek, what does it mean to be to the last byte of a file?

This question popped up in a quiz:

Replace ‘Z’ with the proper number to set the FILE pointer ‘fp’ to the last byte of the file:

fseek( fp, Z, SEEK_END );

My answer was -1 because to me, it seemed natural that being at a certain byte meant being at a position such that appending to that file would overwrite that byte keep storing other bytes if any. The teacher insists on 0. I would appreciate if you could explain.

Upvotes: 0

Views: 1679

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 222437

-1 appears to be correct. I just tried it, and an fgetc after fseek(f, -1, SEEK_END) produced the last character of the file. Seeking to 0 relative to SEEK_END resulted in fgetc returning EOF.

Clearly pointing to the last byte of a file is different from pointing to the end of the file. Furthermore, if there is any ambiguity in terminology, it is a failure of the test, not of the student. If the student has demonstrated the expected knowledge, they should receive credit.

Upvotes: 2

Related Questions