My program makes one image, then has a segmentation fault.
Using GDB, I can see that the segmentation fault happens on the next iteration of the loop after creating the first jpeg.
Looking more, I see that the segmentation fault happens in the middle of reading the next 512 byte block into the buffer (about 395 bytes in).
It reads 512 bytes (one at a time) into this buffer 30k+ times before this, and creates the first image correctly. I think I am either getting to the end of the file, or there is something wrong with how it’s reading in.
Here’s what I've got going on:
//buffer to read block into from our raw file.gdb
BYTE * buffer = malloc(sizeof(BYTE) * 512);
while (1)
{
//break if at end
if (feof(file))
{
break;
}
//reads block into buffer
for (int buffread = 0; buffread < 512; buffread++)
{
fread(&buffer[buffread], sizeof(BYTE), 1, file);
}
//if first part of buffer matches with signature, then make a new jpeg
if ((buffer[0] == 0xff) && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && ((buffer[3] == 0xe0) || (buffer[3] == 0xe1)))
{
//create new file
new_jpeg = fopen(jpeg_name, "w");
//check to make sure file isn't null?
if (new_jpeg == NULL)
{
printf("Didn't work. file is null.");
return 1;
}
//copy buffer into new file
for (int i = 0; i < 512; i++)
{
fwrite(&buffer[i], sizeof(BYTE), 1, new_jpeg);
}
//read 2047 more blocks into it
for (int i = 0, blocks = (2047 * 512); i < blocks; i++)
{
fread(&buffer, sizeof(BYTE), 1, file);
fwrite(&buffer, sizeof(BYTE), 1, new_jpeg);
}
//close the file
fclose(new_jpeg);
//update jpeg_ctr
jpeg_ctr++;
//make new string for next jpeg name
sprintf(jpeg_name, "%03d.jpg", jpeg_ctr);
}
}
If anyone could help I would really appreciate it!