我試圖將一個小型數據分析程序從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