32 bit Windows and the 2GB file size limit (C with fseek and ftell)

后端 未结 4 599
情书的邮戳
情书的邮戳 2020-12-17 03:28

I am attempting to port a small data analysis program from a 64 bit UNIX to a 32 bit Windows XP system (don\'t ask :)). But now I am having problems with the 2GB file size l

相关标签:
4条回答
  • 2020-12-17 04:10

    sorry for not posting sooner but I have been preoccupied with other projects for a while. The following solution works:

    __int64 nsamples(char* filename)
    {
      int fh;
      __int64 n;
    
      /* Open file */
      fh = _open( filename, _O_BINARY );
    
      /* Find end of file */
      n = _lseeki64(fh, 0, SEEK_END);
    
      /* Close file */
      _close(fh);
    
     return n / sizeof(short);
    }
    

    The trick was using _open instead of fopen to open the file. I still don't understand exactly why this has to be done, but at least this works now. Thanks to everyone for your suggestions which eventually pointed me in the right direction.

    0 讨论(0)
  • 2020-12-17 04:21

    My BC says:

    520093696 + 4294967296 => 4815060992

    I'm guessing that your print routine is 32-bit. Your offset returned is most likely correct but being chopped off somewhere.

    0 讨论(0)
  • 2020-12-17 04:28

    And for gcc, see SO question 1035657. Where the advice is compile with the flag -D_FILE_OFFSET_BITS=64 so that the hidden variable(s) (of type off_t) used by the f-move-around functions is(are) 64-bits.

    For MinGW: "Large-file support (LFS) has been implemented by redefining the stat and seek functions and types to their 64-bits equivalents. For fseek and ftell, separate LFS versions, fseeko and ftello, based on fsetpos and fgetpos, are provided in LibGw32C." (reference). In recent versions of gcc, fseeko and ftello are built-in and a separate library is not needed.

    0 讨论(0)
  • 2020-12-17 04:29

    There are two functions called _fseeki64 and _ftelli64 that support longer file offsets even on 32 bit Windows:

    int _fseeki64(FILE *stream, __int64 offset, int origin);
    
    __int64 _ftelli64(FILE *stream);
    
    0 讨论(0)
提交回复
热议问题