ftell at a position past 2GB

后端 未结 4 667
孤独总比滥情好
孤独总比滥情好 2020-12-03 22:19

On a 32-bit system, what does ftell return if the current position indicator of a file opened in binary mode is past the 2GB point? In the C99

4条回答
  •  甜味超标
    2020-12-03 22:45

    At least on a 32bit OS ftell() it will overflow or error or simply run into Undefined Behaviour.

    To get around this you might like to use off_t ftello(FILE *stream); and #define _FILE_OFFSET_BITS 64.

    Verbatim from man ftello:

    The fseeko() and ftello() functions are identical to fseek(3) and ftell(3) (see fseek(3)), respectively, except that the offset argument of fseeko() and the return value of ftello() is of type off_t instead of long.

    On many architectures both off_t and long are 32-bit types, but compilation with

       #define _FILE_OFFSET_BITS 64
    

    will turn off_t into a 64-bit type.


    Update:

    According to IEEE Std 1003.1, 2013 Edition ftell() shall return -1 and set errno to EOVERFLOW in such cases:

    EOVERFLOW

    For ftell(), the current file offset cannot be represented correctly in an object of type long.

提交回复
热议问题