0

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!

1 Answer 1

0
//read 2047 more blocks into it 
    for (int i = 0, blocks = (2047 * 512); i < blocks; i++)

where are you getting this hard-coded figure from?

Also, I would suggest that your fread() should read in 512 bytes at a time, rather than 1 byte at a time. That many reads will probably make check50 kill your program when it checks it as it will probably take many seconds to run.

4
  • Hm, I was thinking that since the buffer had the first of 2048 512-byte blocks, that if a jpeg signature was found, it should read the rest of the blocks into it. But I will try to read 512 bytes in at a time and update.
    – MrLeo
    Commented Aug 28, 2015 at 3:36
  • but where did you get the number 2048 from?
    – curiouskiwi
    Commented Aug 28, 2015 at 3:38
  • per the pset specs: "The implication is that these cameras only write to those cards in units of 512 B. A photo that’s 1 MB (i.e., 1,048,576 B) thus takes up 1048576 ÷ 512 = 2048 "blocks" on a CF card." so, the 512 bytes in the buffer was the first of 2048 blocks, leaving 2047 more. I am having a hard time implementing something here with either fread(&buffer, sizeof(BYTE) * 512, 1, infile) or fread(&buffer, sizeof(BYTE), 512, infile). My if() statement is causing a segmentation fault there, because it's not able to read each individual char out of the buffer. Still working on it!
    – MrLeo
    Commented Aug 28, 2015 at 4:03
  • yes, but why the assumption that any of the jpgs will be 1MB long? I don't think any of them are. and fread(&buffer, 512, 1, infile) should work fine.
    – curiouskiwi
    Commented Aug 28, 2015 at 4:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .