I have a function that reads a rom file into memory and automatically allocates to fit the file but every time I try to read from the file descriptor, fread() returns zero. I’m unsure what I’m doing wrong. can anybody help?
int read_rom(const char *filename, unsigned char ***rom) {
int rom_size;
FILE *fd = NULL;
if((fd = fopen(filename, "r")) != NULL) {
fseek(fd, 0, SEEK_END);
rom_size = ftell(fd);
(*rom) = malloc(rom_size);
int read = fread(rom, 1, rom_size, fd);
fclose(fd);
printf("read: %d\n", read);
return rom_size;
}
return -1;
}
int main(int argc, char **argv) {
unsigned char rom_size = 0;
unsigned char **rom = NULL;
rom_size = read_rom(argv[1], &rom);
return 1;
}
Any takers?
You aren’t reading anything in because you
fseeked to the end of the file. Do afseek(fd, 0, SEEK_SET);to return to the beginning of the file before youfread.