在Windows _fseeki64不寻求正确SEEK_END大文件 [英] On windows _fseeki64 does not seek to SEEK_END correctly for large files

查看:3623
本文介绍了在Windows _fseeki64不寻求正确SEEK_END大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题归纳为以下基本功能,它应该简单地打印
在该文件中的字节数。

当我执行它的83886080字节(80 MB)的文件它打印正确的号码。
但是对于4815060992字节(GB 4.48),它打印520093696文件是这样低。

这似乎有事情做与 SEEK_END 选项,因为如果我的指针设置为手动4815060992字节(如 _fseeki64(FP(__int64)4815060992,SEEK_SET) _ftelli64 不会返回正确的位置。
因此,一个解决办法是获得正确的文件的大小,而无需使用 SEEK_END ,这是怎么做的?

在code编译32位Windows系统上(因此 __的Int64 _iseeki64 _ftelli64 )使用MinGW。

在短期:我在做什么错在这里。

 无效printbytes(字符*文件名)
{
  FILE * FP;
  __int64 N;
  INT结果;  /* 打开文件 */
  FP = FOPEN(文件名,RB);
  如果(FP == NULL)
  {
    PERROR(错误:无法打开文件\\ n);
    返回-1;
  }  / *查找文件结束* /
  结果= _fseeki64(FP(__int64)0,SEEK_END);
  如果(结果)
  {
    PERROR(错误:未能fseek的\\ n!);
    返回结果;
  }  / *获取的字节数* /
  N = _ftelli64(FP);  的printf(%I64d的\\ N,N);  / *关闭文件* /
  FCLOSE(FP);
}


解决方案

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

  __的Int64 nsamples(字符*文件名)
{
  INT FH;
  __int64 N;  /* 打开文件 */
  FH = _open(文件名,_O_BINARY);  / *查找文件结束* /
  N = _lseeki64(FH,0,SEEK_END);  / *关闭文件* /
  _close(FH); 返回N /的sizeof(短);
}

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

I have reduced the problem to the following basic function which should simply print the number of bytes in the file.

When I execute it for a file of 83886080 bytes (80 MB) it prints the correct number. However for a file of 4815060992 bytes (4.48 GB) it prints 520093696 which is way to low.

It seems to have something to do with the SEEK_END option because if I set the pointer to 4815060992 bytes manually (e.g. _fseeki64(fp, (__int64)4815060992, SEEK_SET) _ftelli64 does return the correct position. So a workaround would be to get the proper file size without using SEEK_END, how is this done?

The code is compiled on a 32 bit Windows system (hence __int64, _iseeki64 and _ftelli64) with MinGW.

In short: what am I doing wrong here?

void printbytes(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 bytes */
  n = _ftelli64(fp);

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

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

解决方案

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. (this is a copy of the answer to related question number 4003405).

这篇关于在Windows _fseeki64不寻求正确SEEK_END大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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