Be careful about the return value of the fseek Function

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.