我试图将一个小型数据分析程序从64位UNIX移植到32位Windows XP系统(不要问:))。 但是现在我遇到了2GB文件大小限制问题(在此平台上不是64位)。32位Windows和2GB文件大小限制(C与fseek和ftell)
我搜查了这个网站和其他可能的解决方案,但找不到任何可以直接翻译我的问题。 问题在于使用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字节我得到样本文件(例如_ftelli64
给出字节)这很奇怪。
奇怪的是,当我离开(__int64)
转换为_fseeki64
时,我得到一个运行时错误(无效参数)。
任何想法?
是Win32 API的一个选项吗? – 2010-10-23 10:01:29
你使用什么编译器? GCC?视觉(东西)?还有别的吗? – 2010-10-26 00:01:01
我正在使用MinGW(“不能”使用VS,因为我正在编写的函数是f2py Python扩展模块的一部分)。 Win32 API可能是一个选项,如果它可以很容易地集成到这个函数中而不会增加许多依赖(你可能会告诉我对Windows不熟悉)) – 2010-10-27 14:19:48