Question about file seeking position

做~自己de王妃 提交于 2019-12-01 09:44:33

The offset parameter of lseek is of type off_t. In 32-bit compilation environments, this type defaults to a 32-bit signed integer - however, if you compile with this macro defined before all system includes:

#define _FILE_OFFSET_BITS 64

...then off_t will be a 64-bit signed type.

For fseek, the fseeko function is identical except that it uses the off_t type for the offset, which allows the above solution to work with it too.

a 4 byte unsigned integer can represent a value up to 4294967295, which means if you want to move more than 4G, you need to use lseek64(). In addition, you can use fgetpos() and fsetpos() to change the position in the file.

On Windows, use _lseeki64(), on Linux, lseek64().

I recommend to use lseek64() on both systems by doing something like this:

#ifdef _WIN32
#include <io.h>
#define lseek64 _lseeki64
#else
#include <unistd.h>
#endif

That's all you need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!