Janjan
Janjan

Reputation: 85

fseek and fwrite past EOF

Let us first assume I have a 20 byte binary file consisting of 5 integers. Now let us say that I tried to:

fseek(fp, 2*sizeof(int), SEEK_END)

and I tried to call:

fwrite(&i, sizeof(int), 1, fp);

Is this even possible?

If it is possible will the new binary file be of size 32 or will it simply be of size 24? If is it of 32, what is the content of the binary file from position 20 to 27?

Upvotes: 2

Views: 416

Answers (1)

Danny_ds
Danny_ds

Reputation: 11406

According to this:

  • Library implementations are allowed to not meaningfully support SEEK_END (therefore, code using it has no real standard portability).

And here:

POSIX allows seeking beyond the existing end of file. If an output is performed after this seek, any read from the gap will return zero bytes. Where supported by the filesystem, this creates a sparse file.

On a my testsystem seeking past SEEK_END fills the remainder of the file with nullbytes upto the new file pointer.

Upvotes: 4

Related Questions