我在 VirtualBox 中的 Windows XP 上遇到了非常奇怪的问题。
ReadFile()
函数拒绝在单次调用中读取超过 16Mb 的数据。它返回错误代码 87 ( ERROR_INVALID_ARGUMENT
)。看起来数据长度限制为 24 位。
这是允许我找出确切限制的示例代码。
#include <conio.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
int _tmain(int argc, _TCHAR* argv[])
{
int fd,len,readed;
char *buffer;
char *fname="Z:\\test.dat";
fd=_open(fname,_O_RDWR|_O_BINARY,_S_IREAD|_S_IWRITE);
if (fd==-1) {
printf("Error opening file : %s\n",strerror(errno));
getch();
return -1;
}
len=_lseek(fd,0,SEEK_END);
_lseek(fd,0,SEEK_SET);
if (!len) {
printf("File length is 0.\n");
getch();
return -2;
}
buffer=(char *)malloc(len);
if (!buffer) {
printf("Failed to allocate memory.\n");
getch();
return -3;
}
readed=0;
while (readed<len) {
len-=100;
readed=_read(fd,buffer,len);
if (len<=100) break;
}
if (readed!=len) {
printf("Failed to read file: result %d error %s\n",readed,strerror(errno));
getch();
return -4;
}
_close(fd);
printf("Success (%u).",len);
getch();
return 0;
}
文件Z:\test.dat
长度为 21Mb。
结果是“ Success (16777200).
”
我试图在谷歌中找到同样的问题但没有成功:(
可能有人知道问题的原因是什么?