32位Windows和2GB的文件大小限制(C fseek的带和FTELL) [英] 32 bit Windows and the 2GB file size limit (C with fseek and ftell)

查看:628
本文介绍了32位Windows和2GB的文件大小限制(C fseek的带和FTELL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图端口一个小的数据分析程序从64位UNIX到32位的Windows XP系统(不要问:))。
但现在我有2GB的文件大小限制的问题(没有长为64位在此平台上)。

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 limit (long not being 64 bit on this platform).

我已经搜索这个网站和其他可能的sollutions,但找不到任何直接翻译我的问题。
问题是,在使用fseek的和FTELL的。

I have searched this website and others for possible sollutions but cannot find any that are directly translatable to my problem. The problem is in the use of fseek and ftell.

有谁知道修改以下两个功能,使它们在32位Windows XP连续工作超过2GB(实际上订购100GB)。

Does anyone know of a modification to the following two functions to make them work on 32 bit Windows XP for files larger than 2GB (actually order 100GB).

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

It is vital that the return type of nsamples is a 64 bit integer (possibly 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:

I tried using _fseeki64 and _ftelli64 using the following to replace 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 字节),这是奇怪的。

for a file of 4815060992 bytes I get 260046848 samples (e.g. _ftelli64 gives 520093696 bytes) which is strange.

奇怪的是,当我离开了在呼叫中的(__的Int64)投地 _fseeki64 我得到一个运行时错误(无效参数)。

Curiously when I leave out the (__int64) cast in the call to _fseeki64 I get a runtime error (invalid argument).

任何想法?

推荐答案

抱歉没有张贴越早但我一直preoccupied与其他项目了一段时间。
下面的解决方案如下:

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);
}

诀窍是使用 _open 而不是的fopen 来打开该文件。
我还是不明白究竟为什么有许多工作要做,但至少这个工程现在。
感谢大家对你的建议,最终我指出了正确的方向。

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.

这篇关于32位Windows和2GB的文件大小限制(C fseek的带和FTELL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆