Get the length of any file in C (may be greater than 2GB) #define _FILE_OFFSET_BITS 64

Source: Internet
Author: User

After opening the file, use the fseek () function to move the file position pointer to the end of the file, using Ftell () to get the position pointer from the file header bytes, the number of bytes is the length of the file. However, this is also subject to the following restrictions: The return value of the Ftell () function is long, under Windows (either 32-bit or 64-bit), the long variable has a length of 4 bytes, so that the file length that is normally obtained must be less than 2GB.

Microsoft has provided functions _fseeki64 () and _ftelli64 () in stdio.h in the development tools for Visual Studio 2005 and later versions. The variable that represents the offset of the file pointer in the _fseeki64 () function is the __int64 type, and the return value of _ftelli64 () is also the __int64 type, so you can use these two functions to get the length of a large file (more than 2GB).

Note that the C compiler in Linux and Unix does not provide the _fseeki64 () and _ftelli64 () functions, but the Fseeko () and Ftello () functions are provided in stdio.h, and the Fseeko () function represents the change in the offset of the file pointer The amount is the off_t type, the return value of Ftello () is also off_t type, the length of the off_t type may be 32-bit or 64-bit, using a macro definition #define _FILE_OFFSET_BITS 64 can set off_t to 64.

Here is an example of an implementation that uses C to get the length of a file (which may be greater than 2GB), which can be used on both Windows and Linux platforms (not tested on Unix), including three files:

Http://itlab.idcquan.com/c/skill/943552.html

Get the length of any file in C (may be greater than 2GB) #define _FILE_OFFSET_BITS 64

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.