【发布时间】:2012-02-09 17:48:48
【问题描述】:
我正在尝试使用 Visual C++ 以二进制模式读取整个 jpg 文件。代码如下:
FILE *fd = fopen("c:\\Temp\\img.jpg", "rb");
if(fd == NULL) {
cerr << "Error opening file\n";
return;
}
fseek(fd, 0, SEEK_END);
long fileSize = ftell(fd);
int *stream = (int *)malloc(fileSize);
cout << fileSize << '\n';
fseek(fd, 0, SEEK_SET);
int bytes_read = fread(stream, fileSize, 1, fd);
printf("%i\n", bytes_read);
fclose(fd);
问题在于bytes_read 始终为1。fileSize 变量包含正确的文件大小。所以我不确定为什么 bytes_read 总是 1 并且不等于 fileSize..?
【问题讨论】:
标签: c++ visual-c++