I have written the following code to read the information from the RAW file and try to sort it out into the proper jpeg files.

Through gdb, I can see that the file is reading into the buffer fine (and there is some very interesting info hidden in there). The problem is that the loop only runs three times before exiting "normally" according to GDB, but it is exiting during the 'if' condition to check for a jpeg_sig, which doesn't make sense.

When I run the program outside of GDB then there is no error message. I can't tell why it the loop is not running until the break that I put in at the top of the while loop. Can someone help? The rest of the while loop is what happens if a JPEG signature is found -- if not, it just loops back up.

while (1)
    //checks to make sure end of file hasn't been reached by reading a char
    char check = fgetc(file);
    if (check == feof(file))

    //if not reached, seeks back
    fseek(file, -(sizeof(char)), SEEK_CUR);

    //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)))

1 Answer 1


Without the full code I can't be sure, but I guess it because the following line:

if (check == feof(file))

According to feofs man page:

int feof(FILE *stream);

The function feof() tests the end-of-file indicator for the stream pointed to by stream, returning nonzero if it is set.

So I guess what happens, is the the char check is equal with 0, which is what feof returns if the end of the file hasn't been reached. Change the condition just to:

if (feof(file))

So when it returns a non-zero integer, which it does when the end is reached, the condition will be true, and the inside of the if will execute.

If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.

  • 1
    This fixed it! Now it's making a jpeg and I'm also getting a segmentation fault -- I will get into it again from there. Thanks!
    – MrLeo
    Aug 21, 2015 at 18:54

You must log in to answer this question.

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