32位Windows和2GB文件大小限制(C与fseek和ftell)

我试图将一个小型数据分析程序从64位UNIX移植到32位Windows XP系统(不要问:)).
但是现在我遇到了2GB文件大小限制的问题(在这个平台上长时间不是64位).

搜索了这个网站和其他人可能的解决方案,但找不到任何可以直接翻译我的问题.
问题在于使用fseek和ftell.

有没有人知道对以下两个函数修改,使它们可以在32位Windows XP上运行大于2GB的文件(实际上订购100GB).

至关重要的是,nsamples的返回类型是64位整数(可能是int64_t).

long nsamples(char* filename)
{
  FILE *fp;
  long n;

  /* Open file */
  fp = fopen(filename,"rb");

  /* Find end of file */
  fseek(fp,0L,SEEK_END);

  /* Get number of samples */
  n = ftell(fp) / sizeof(short);

  /* Close file */
  fclose(fp);

  /* Return number of samples in file */
  return n;
}

void readdata(char* filename,short* data,long start,int n)
{
  FILE *fp;

  /* Open file */
  fp = fopen(filename,"rb");

  /* Skip to correct position */
  fseek(fp,start * sizeof(short),SEEK_SET);

  /* Read data */
  fread(data,sizeof(short),n,fp);

  /* Close file */
  fclose(fp);
}

我尝试使用_fseeki64和_ftelli64使用以下代码替换nsamples:

__int64 nsamples(char* filename)
{
  FILE *fp;
  __int64 n;
  int result;

  /* Open file */
  fp = fopen(filename,"rb");
  if (fp == NULL)
  {
    perror("Error: Could not open file!\n");
    return -1;
  }

  /* Find end of file */
  result = _fseeki64(fp,(__int64)0,SEEK_END);
  if (result)
  {
    perror("Error: fseek Failed!\n");
    return result;
  }

  /* Get number of samples */
  n = _ftelli64(fp) / sizeof(short);

  printf("%I64d\n",n);

  /* Close file */
  fclose(fp);

  /* Return number of samples in file */
  return n;
}

对于4815060992字节的文件,我得到260046848个样本(例如_ftelli64给出520093696字节),这很奇怪.

奇怪的是,当我在_fseeki64的调用中省略(__int64)强制转换时,我得到一个运行时错误(无效的参数).

有任何想法吗?

有两个名为_fseeki64和_ftelli64的函数,即使在32位Windows上也支持更长的文件偏移量:
int _fseeki64(FILE *stream,__int64 offset,int origin);

__int64 _ftelli64(FILE *stream);

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...