I am trying to read an entire jpg file in binary mode using visual c++. The code is as follows:
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);
The problem is that the bytes_read
is always 1. The fileSize
variable contains the correct size of the file. So I am not sure why the bytes_read
is always 1 and not equal to fileSize..?