替换不同平台中的fpos_t和fpos_t._POS

replace fpos_t and fpos_t._pos in different platform

本文关键字:fpos POS 平台 替换      更新时间:2023-10-16

In Linux(ubuntu) fpos_t is in a struct

typedef fpos_t {
    size_t _pos,
    size_t _state
}

在 Windows 中,未定义fpos_t。在代码库中,它是以这种方式定义的。

#ifndef _FPOS_T_DEFINED
typedef __int64 fpos_t;
#define _FPOS_T_DEFINED
#endif  /* _FPOS_T_DEFINED */

但是,在我的代码中

foo () {
    fpos_t fsize;
    fgetpos(fp, &fsize);    // Function A
    bar.len = (unsigned int)fsize;                      // for windows  Function B      
    bar.body = (char *)SAFE_MALLOC( (size_t)fsize);     // for windows
    //bar.len = (unsigned int)fsize._pos;                   // for linux Function B
    //bar.body = (char *)SAFE_MALLOC( (size_t)fsize._pos);// for linux
}
问题是,在不同

平台上编译时,将fsize._pos替换为 fsize 的优雅方法是什么。我知道的方法

#if defined(LINUX)
    //code for linux
#else 
    //code for windows
#endif 

fpos_t结构被定义为不透明;没有可移植的方法可以从fpos_t中提取文件偏移量(将fpos_t转换为 int 或 char)。

如果要获取文件位置的字节偏移量,请在二进制模式下打开文件并使用ftello(ftello/fseeko vs fgetpos/fsetpos)。