Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / MFC
Article

Bypassing ftell/fseek's 2gb limitation

Rate me:
Please Sign up or sign in to vote.
4.90/5 (14 votes)
29 Jan 20052 min read 90.6K   15   15
Instructions on how to implement _fseeki64 and _ftelli64.

Introduction

Recently, I was working on completing a port of a Linux program to Windows. It's called mpgtx, and is a command line MPEG editing program. The port was pretty easy, since it didn't use many Linux-specific functions (and someone had already done part of the port), but I did run across one big problem: it would fail on any long MPEG-2 clip. I quickly realized that it was because the fseek/ftell functions use the data type long for file offsets, which is a 32-bit signed integer on most systems. Therefore, those functions don't work on files that are bigger than 2048 megabytes. I then searched around for information on getting around that problem, and the only info I found was on Linux-specific functions (fseeko/ftello) or a non-buffered function (_lseeki64). I didn't want to use Cygwin, and I really didn't want to convert all the fopen/fread/fseek/etc... function calls to _open/_read/_lseeki64/etc... and still maintain code compatibility with Linux, so I had to find another way. I thought that because there was a _lseeki64 function there might be a fseeki64 function, but nothing was mentioned about that function in the Help system and hardly anything was listed when searching Google for it. One thing Google did find, though, was a page that said there was a file named "fseeki64.c" in "crt/src". Sure enough, both the fseeki64.c and ftelli64.c files existed in my "Microsoft Visual Studio .NET 2003\Vc7\crt\src" folder. Unfortunately, neither function is mentioned in stdio.h, so it's going to take a little extra work to use those functions...

Using the code

First, you'll have to add the following imports into your code:

extern "C" int __cdecl _fseeki64(FILE *, __int64, int);
extern "C" __int64 __cdecl _ftelli64(FILE *);

Note that they use __in64 for the file offset data type, instead of long.

Next, replace all calls to fseek and ftell with _fseeki64 and _ftelli64, respectively. Finally, change all the variables that you use to hold file offsets from long to __int64. That's it; now you should be good to go!

Points of Interest

Does anyone know why _fseeki64 and _ftelli64 are completely, mysteriously undocumented?

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I started programming by getting books on BASIC programs from the local library and entering them into QBASIC, and when I was 10 I bought a copy of Visual Basic 5 and started visual development on my 33mhz (talk about long compile times!). After a couple years I decided to learn the language that most professional programs and games were made in: C++. As soon as I learned about the power of pointers, I was addicted to programming for life Wink | ;) .

I've worked on a number of programs since then, including IGCutter, Pockulous, and Freelancer Mod Manager. I've also had the great opportunity to intern at Adobe over a summer; definitely a dream come true Smile | :) .

I'm currently a freshman/sophomore in college, trying to stay awake in boring required classes on Java...

-Matthew Lieder

Comments and Discussions

 
GeneralVisual Studio 2005 (VC8) appears to define _fseeki64 and ftelli64 in stdio.h now Pin
Paul "TBBle" Hampson15-Apr-08 23:26
Paul "TBBle" Hampson15-Apr-08 23:26 
GeneralLarge File Programing Pin
HsMow24-Nov-06 3:21
HsMow24-Nov-06 3:21 
QuestionHow do I link _ftelli64 against the multithreaded dll version of the c library? Pin
poke53281,027-Jul-06 23:30
poke53281,027-Jul-06 23:30 
Generalonly available in single-thread library Pin
wzhao200025-May-05 5:37
wzhao200025-May-05 5:37 
GeneralRe: only available in single-thread library Pin
wzhao200025-May-05 6:00
wzhao200025-May-05 6:00 
Generalsome defines Pin
Vlad Vissoultchev31-Jan-05 2:17
Vlad Vissoultchev31-Jan-05 2:17 
GeneralRe: some defines Pin
IGx8931-Jan-05 3:20
IGx8931-Jan-05 3:20 
GeneralRe: some defines Pin
zap46024-Aug-07 4:57
zap46024-Aug-07 4:57 
GeneralSetFilePointer/SetFilePointerEx Pin
gnk30-Jan-05 10:50
gnk30-Jan-05 10:50 
GeneralRe: SetFilePointer/SetFilePointerEx Pin
IGx8930-Jan-05 10:56
IGx8930-Jan-05 10:56 
GeneralRe: SetFilePointer/SetFilePointerEx Pin
MatrixDud8-Mar-06 3:13
MatrixDud8-Mar-06 3:13 
GeneralRe: SetFilePointer/SetFilePointerEx Pin
bytetracer24-Mar-07 20:48
bytetracer24-Mar-07 20:48 
GeneralRe: SetFilePointer/SetFilePointerEx Pin
bytetracer24-Mar-07 21:05
bytetracer24-Mar-07 21:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.