Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am making a program that reads disk images in C. I am trying to make something portable, so I do not want to use too many OS-specific libraries. I am aware there are many disk images that are very large files but I am unsure how to support these files.

I have read up on fseek and it seems to use a long int which is not guaranteed to support values over 231-1. fsetpos seems to support a larger value with fpos_t but an absolute position cannot be specified. I have also though about using several relative seeks with fseek but am unsure if this is portable.

How can I support portably support large files in C?

like image 434
ZeroKelvinKeyboard Avatar asked Jun 05 '15 03:06

ZeroKelvinKeyboard


2 Answers

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

like image 161
Severin Pappadeux Avatar answered Sep 21 '22 12:09

Severin Pappadeux


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.

like image 45
StilesCrisis Avatar answered Sep 21 '22 12:09

StilesCrisis