Can I seek a position beyond 2GB in C using the standard library?

拜拜、爱过 提交于 2019-12-01 05:14:30
Severin Pappadeux

There is no portable way.

On Linux there are fseeko() and ftello(), pair (need some defines, check ftello()).

On Windows, I believe you have to use _fseeki64() and _ftelli64()

#ifdef is your friend

StilesCrisis

pread() works on any POSIX-compliant platform (OS X, Linux, BSD, etc.). It's missing on Windows but there are lots of standard things that Windows gets wrong; this won't be the only thing in your codebase that needs a Windows special case.

You can't do it with standard C. Even with relative seeks it's not possible on some architectures.

One approach would be to check the platform at compile time. You can just check the value of LONG_MAX and throw a compile error if it's not large enough. But even that doesn't guarantees that the underlying filesystem supports files larger than 2 or 4GB.

A better way is to use the pre-processor macros supplied by your compiler to check the operating system that your code is being compiled for and write operating system specific specific. The operating system should provide a way to check that the filesystem actually supports files larger than 2GB or 4GB.

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