c++file-io

size of off_t at compilation time


After reading numerous report on stackoverflow, I decide to go with FILE* (over std::fstream) to manipulate quickly my binary files. Furthermore, I needed to truncate my file during the process (_chsize or ftruncate), which will work only with a file descriptor (fileno call).

So I am trying to have something like this working (C++):

#define _FILE_OFFSET_BITS 64
#include <stdio.h>
static inline off_t FTello(FILE *stream)
{
#if _WIN32
#if defined(__MINGW32__)
  return ftell( stream ); // 32bits
#else
  return _ftelli64( stream );
#endif
#else
  return ftello( stream );
#endif
}

However this is clearly not working on Visual Studio. I could not find anywhere in the documentation a way to change the size of off_t at compilation time.

It would be tempting to have a signature like this (I assume I have C99: stdint.h):

static inline int64_t FTello(FILE *stream)

What others have been doing ?

For reference, here is what I have been reading, which seems to indicate FILE* provide better performance:


Solution

  • If you are already enforcing large file support, just define FTello() to return int64_t.

    You might find inttypes.h more portable than stdint.h though.

    MS is not famous for being compatible with UNIX, even though they provide similarly named functions. If you are going to provide a compatibility layer, better look into what existing layers do (e.g. boost.filesystem), there are subtleties everywhere.

    By the way, boost.filesystem has resize_file().