Sometimes, when the code is correct in the normal programming era, we think that the return value of fseek is either 0 or other, that is, if seek files return a value of FI 0. In fact, I didn't care too much about it before, and I didn't have an extreme test. In order to ensure the robustness of the code, I discussed it with my colleagues and found that the fseek function is far from returning as I imagined.
Moves the file pointer to a specified location.
Int fseek (File * stream, long offset, int origin );
Int _ fseeki64 (File * stream, _ int64 offset, int origin );
The pointer offset of the corresponding file is not clearly described in the function definition. When the offset is a positive number, it is offset to the end of the file, when it is a negative number, it is offset to the file header. Here, we need to pay attention to the relationship between the offset size and the file size boundary.
When offset is an offset to the end of a file, fseek returns 0 regardless of whether the offset exceeded the end of the file. When the offset does not exceed the end of the file, the file Pointer Points to the normal offset address. When the offset exceeds the end of the file, the file Pointer Points to the end of the file. The offset error-1 value is not returned.
When offset is offset to the file header, if the offset does not exceed the file header, it is normal offset. The file Pointer Points to the correct offset address, and the return value of fseek is 0. when the offset value exceeds the file header, fseek returns the error-1 value, and the file pointer remains unchanged at the original address.