I have been tasked with implementing an ls function for files in an .iso file system. I need to use fread but it is segfaulting. I know the file exists and i know it has data in it. I have the format for calling fread and I have set it up as follows
static void ls(File *file, int32_t root_dir_size)
{
void* sector;
int result;
result = fread(sector, 1, 1, file);
}
Why does this segfault? I am new to C and I am coming from C++ so not sure what I am missing here.
fread
needs memory into which to put the data it's reading. That memory is pointed to by the first parameter, in your case sector
. However, you have not initialized sector
. You will need to allocate some memory, in this case just one byte and have sector
point to that. For instance:
void *sector = malloc(1);
As always, don't forget to free the memory after you're done using it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.