Search code examples
cfilepositioningbinaryfilesfseek

Why doesn't this purposely made invalid file positioning not bringing up any errors


So given a binary file containing 100 bytes. I'm pretty sure that the INVALID ones should cause and error, but why doesn't it? I'm confused the once in INVALID should bring up a semantic error right? Or am I misunderstanding something

/* VALID */  fseek(fp, sizeof(char) * 2, SEEK_SET);
/* VALID */  fseek(fp, -2 * sizeof(char), SEEK_END);
/* INVALID */fseek(fp, sizeof(char)* 2, SEEK_END);
/* INVALID */fseek(fp, -2 * sizeof(char), SEEK_SET);
/* INVALID */fseek(fp, 50, SEEK_CUR);
/* VALID */  fseek(fp, -50, SEEK_CUR);
/* INVALID */ fseek(fp, 51, SEEK_CUR);
/* INVALID */ fseek(fp, -51, SEEK_CUR);

Solution

  • What "semantic error" are you talking about?

    Invalid file position will cause fseek to fail, which will be indicated by the return value of fseek. You have to receive that value and analyze it to determine whether the function succeeded or not. Your code above completely ignores the return value of fseek.

    Keep in mind that the standard library does not require all platforms to support SEEK_END positioning. The platform might not know where exactly the end of file is (until you actually bump into it while reading the file). For example, some file systems might not store the exact file size in bytes, but rather store how many "blocks" or "sectors" that file occupies, which can only serve as approximate file size. For this reason the language specification states that SEEK_END positioning is not guaranteed to be meaningfully supported.

    Also keep in mind that one some platforms it might be perfectly allowable to seek past the end of file opened for writing. The subsequent write operation will write data at the new position, while filling the "gap" with zeros.