【发布时间】:2010-10-23 09:56:02
【问题描述】:
我正在尝试将小型数据分析程序从 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) 演员表时,我得到一个运行时错误(无效参数)。
有什么想法吗?
【问题讨论】:
-
你用的是什么编译器?海合会?视觉(东西)?还有什么?
-
我正在使用 MinGW(“不能”使用 VS,因为我正在编写的函数是 f2py Python 扩展模块的一部分)。如果 Win32 API 可以很容易地集成到这个函数中而不添加许多依赖项,那么它可能是一个选项(你可能会说我对 Windows 不太熟悉 :))
-
我也发布了一个更具体的问题,如果得到回答,我也会在这里添加最终解决方案。
标签: c windows porting large-files c99