0

The program crashes in the first time fread is called I tracked it with gdb, This is the error: Program received signal SIGSEGV, Segmentation fault. __GI__IO_fread (buf=0xbfffeedc, size=512, count=1, fp=0x6765706a) at iofread.c:41 41 iofread.c: No such file or directory.

and here's my code, thanks for your time:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char* argv[])
{
    //open the file
    FILE* raw = fopen ("card.raw","r");

if (raw == NULL)
{
    printf("Could not open file\n");
    return 2; 
}
char name[4];
int num=0;
sprintf(name,"%03d.jpeg", num);
uint8_t block [512]={0};
FILE* pic;

while(fread(&block,512,1,raw)==512)
{
    if(((*block== 0xff && *(block+1)== 0xd8 && *(block+2)== 0xff) && ((*(block+3)== 0xe0)||*(block+3)== 0xe1)) && num==0 )
    {
        sprintf(name,"%03d.jpeg", num);
        pic = fopen (name, "a");
        num++;
    }

    else if(((*block== 0xff && *(block+1)== 0xd8 && *(block+2)== 0xff) && ((*(block+3)== 0xe0)||*(block+3)== 0xe1)))
    {
        fclose(pic);
        sprintf(name,"%03d.jpeg", num);
        pic = fopen (name, "a");
        num++;
    }
    if(num>0)
    {
        fwrite(&block,512,1,pic);
    }

}  

fclose(pic);
}    
0

You have a couple of problems and they're interacting with each other.

First, you have a couple of problems with the creation of the output filename. The filename should end in 'jpg', not 'jpeg'. Next, you're trying to stuff 9 characters into a 4 character long char array. This is overwriting into other memory, ultimately affecting the buffer.

Next, you have two flaws in your fread, but I'll let you find them on your own. You should start by rereading the spec for fread() and it's parameters carefully. Can't steal all the debugging fun from you! ;-)

If this answers your question, please click the check mark to accept. Let's keep up on forum housekeeping! ;-)

1
  • Thank you so much! you're the best! – Alya Salman Nov 2 '15 at 0:15

You must log in to answer this question.

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