0

My code seems to have trouble making the .jpg files in which to place the blocks of data.

 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>

uint8_t buffer[512];
uint8_t jpeg1[4] = {0xff, 0xd8, 0xff, 0xe0};
uint8_t jpeg2[4] = {0xff, 0xd8, 0xff, 0xe1};

int main(int argc, char* argv[])
{
    //open memory card file - fread the raw data

    int picNum = 0;
    char title[8];
    FILE* img;
    FILE* inptr = fopen("card.raw", "r");
    if(inptr == NULL)
    {   
        fclose(inptr);
        fprintf(stderr, "Wasn't able to open the cardfile.\n");
        return 1;
    }

    do
    {
        fread(&buffer, 1, 512, inptr);    
    }
    while((buffer[0] != jpeg1[0]) && (buffer[1] != jpeg1[1]) && (buffer[2] != jpeg1[2]) && ((buffer[3] != jpeg1[3]) || (buffer[3] != jpeg2[3])) && (fread(buffer, 1, 512, inptr) == 1));
    while(fread(&buffer, 512, 1, inptr) == 1)
    {

        if((buffer[0] == jpeg1[0]) && (buffer[1] == jpeg1[1]) && (buffer[2] == jpeg1[2]) && ((buffer[3] == jpeg1[3]) || (buffer[3] == jpeg2[3])))
        {
            if(img != NULL)
            {
                fclose(img);
                picNum++;
            }
            sprintf(title, "%3d.jpg", picNum);
            img = fopen(title, "a"); 

        }
        fwrite(&buffer, 1, 512, img);
        fread(&buffer, 1, 512, inptr);
    }
    fclose(img);
    return 0;
}

Get this from GDB:

41          if((buffer[0] == jpeg1[0]) && (buffer[1] == jpeg1[1]) && (buffer[2] == jpeg1[2]) && ((buffer[3] == jpeg1[3]) || (buffer[3] == jpeg2[3])))
(gdb) 
52          fwrite(&buffer, 1, 512, img);
(gdb) 

Program received signal SIGSEGV, Segmentation fault.
__GI__IO_fwrite (buf=0x804a044 <buffer>, size=1, count=512, fp=0x1)
    at iofwrite.c:41
41  iofwrite.c: No such file or directory.

Why isn't the jpg being made? Help on fixing this segfault please!

4
  • My first observation is that you haven't declared the jpeg1 array. – ChrisG Jul 25 '15 at 13:49
  • Also that thing buffer[3] != (jpeg1[3] || jpeg2[3]) may seem logical to you, but the compiler will not understand it as you intend. jpeg1[3] || jpeg2[3] is not a valid operation. – ChrisG Jul 25 '15 at 13:51
  • Hey Chris, I did declare them sorry, they aren't in the text. And I fixed the other operand as you said, though it doesn't have any effect on the segfault im listing above. \ – Joey Beaudette Jul 26 '15 at 4:16
  • Update the code above. – ChrisG Jul 26 '15 at 8:14
0

For your do while loop, the very very last block read is the start block of the jpg. However, in the following while loop, you immediately call fread again and thus buffer now points to the 2nd block of the jpg. You then check if this block has the jpg signature, which it doesn't. Thus, the if loop isn't executed and img isn't opened. This creates the error when your try to write to img

1
  • Hmm I guess I misunderstood how using fread within a while loop works; its still a legitimate call. Unfortunately I'm not sure how to fix this. I tried changing the do\while to if, segfault. removed it, segfault. put the while loop in the do/while, segfault. every time because img doesnt exist. – Joey Beaudette Jul 27 '15 at 6:08

You must log in to answer this question.

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