0

According to the unix man pages ftell either returns -1 to indicate an error.

The function definition is:

long int ftell ( FILE * stream );

Where long int is a 32 bit integer (you need long long for 64 bit)

however (-1L) == 0x00000000FFFFFFFF LL which should be a valid value for really large files (round about 4GB) in which case, how do we know whether an error has occurred or we just have a really large file?

doron
  • 27,972
  • 12
  • 65
  • 103
  • You could check `errno` or switch to `off64_t ftello64(FILE *stream)`. Encountering a file which is >2^63 in bytes is at least less possible than one having >2^31 bytes. – Neet Oct 24 '13 at 10:37
  • @Neet put your comment down as an answer. – doron Oct 24 '13 at 10:41
  • "Where long int is a 32 bit integer" - I guess **no.** Nothing guarantees/requires that. On any sensible 64-bit implementation, it's in fact 64 bits long. (You read that right -- Windows does not count as a "sensible implementation".) –  Oct 24 '13 at 12:16
  • Please tell me on what platform long in is 64 bit. I know of none. – doron Oct 24 '13 at 12:41
  • @doron Ever heard of Linux? OS X? iOS (recently)? –  Oct 24 '13 at 19:56
  • @user529758, On a Window machine, `printf("%d\n", (int) sizeof (long));` can readily print 8, even for some years now. The OS does not set the size of `long`, the compiler does. – chux - Reinstate Monica Oct 27 '18 at 22:38

1 Answers1

2

You could either check errno or use the 64 bit version of ftell called off64_t ftello64(FILE *stream).

Neet
  • 3,937
  • 15
  • 18