I had been working on recover for two weeks but it is still not working. My recover program somehow is not finding the start of the jpegs, I don't know what is wrong with it can someone help please! thank you so much!

unsigned char buffer[512]={0}; //declare storage space 

//other starting codes
fread(buffer, 512, sizeof(char), inptr);

//printf for debugging purposes  
 printf("read buffer 0 is %c\n",buffer[0]);
 printf("(read) buffer 1 is %c\n",buffer[1]);
 printf("(read) buffer 2 is %c\n",buffer[2]);
 printf("(read) buffer 3 is %c\n",buffer[3]);

//finds beginning of jpg file           
while ((isjpg(buffer) != 1)&& //if not jpg
(fread(&buffer, sizeof(char), 512, inptr) < 512))//if not at end of file
    fread(buffer, 512, sizeof(char), inptr);//read another unit

//other unrelavent codes above
//is jpg
int isjpg (unsigned char buffer[])
//printf for debugging purposes    

    printf("(0xff) buffer 0 is %c\n",buffer[0]);
    printf("(0xd8) buffer 1 is %c\n",buffer[1]);
    printf("(0xff) buffer 2 is %c\n",buffer[2]);
    printf("(0xe0) (0xe1) buffer 3 is %c\n",buffer[3]);

if  (buffer[0] == 0)
    if (buffer[1] == 'x')
        if (buffer[2] == 'f'|| buffer[2] == 'd' || buffer[2] == 'e')
            if (buffer[3] == 'f' || buffer[3] == 8 || buffer[3] == 0 ||
            ( buffer[3] == 1))
                return 1;//is jpg
            else return 0;  
        else return 0;  
    else return 0;  
else return 0;  

and there are the compiling results COmpile results

also the printf results for the first set of debugging printfs after the fread are as following enter image description here

  • Why are you using a struct?
    – gitterb
    Mar 4, 2015 at 2:52
  • I tried using an unsigned char before but it said something about uncomparable variable type so I decided to make a type myself to silent the warning Mar 4, 2015 at 7:47

2 Answers 2


Edited answer for the new question,

just declare a regular array, not a pointer to one,

unsigned char buffer[512]={0}; //declare storage space

then you can pass the buffer directly to fread, no need to put an address to it fread(buffer)

same for isjpg, array can be passed directly, the compiler will handle


if you have declared the function as

int isjpg(buffer[]);

to make sure it is expecting an unsigned char buffer[] when declaring it. The way you have done it, if you want to pass &buffer to a function, you need to have the function expect a *buffer, not an &buffer.

  • um is it writing to the pinter bf instead of BUFFER itself? – Mar 5, 2015 at 0:11
  • Yeah it is dumping it at the address of the struct, which could work fine but keep it simple as per Kareems suggestion above, and also, as he pointed out, your loop constructs are out of order. Making isjpg() return 0 for true is pretty confusing too, 1 is true, 0 is false normally Mar 5, 2015 at 9:33
  • ok i will fix that but i don't really understand the error of the loop constructions part. can you please explain again? i'm sorry... Mar 5, 2015 at 13:00
  • Because you are using an uninitialized struct, you are testing garbage data against buffer[0,1,2,3]. You should either fire fread before your first check, or declare your array buffer[512] = {0}; so that it starts zero filled. Also, we were confused because your function is named isjpg, but returns 1 when it is not the beginning of a jpeg. 1 == true, and 0 == false in syntax. So you should reverse it and than check while(!isjpg(&bf)) Are you sure your code is not finding the beginnings? Check by putting a printf(found a jpeg) sort of alert under //is jpg here. Mar 5, 2015 at 17:39
  • I updated the code but it said that I cannot use a char* but I changed the rest of the issues. And there are still the problems with that brackes for isjpg, which i am very sure that i closed the brackes Mar 6, 2015 at 8:41

Well, I don't think you need to wrap up your array with a struct at all. You could use the array directly.

Besides, no data is read into bf at all as you never call fread except inside the body of the loop. So the continuation condition is always false; because the fread statement is NEVER reached.


if your isjpg function found a start of a JPG it will return 0 and the loop continuation condition (i.e., isjpg(&bf) == 1 will evaluate to false and the loop will end execution.

  • sorry i am really bad at this, but do you mean that because of there wrong data stored in the buffer the function isjpg doesn't work so fread is always reading the wrong data? can you please explain again, i am sorry i don't really get it... Mar 5, 2015 at 12:59
  • Hey Kareem, the isjpg function returns true when it is not a jpg, and false when it is. It's semantically backwards but technically, that part should work despite being funky. Mar 5, 2015 at 17:31
  • @freshsisyphus Opps! Edited! Thanks for the heads-up!
    – kzidane
    Mar 5, 2015 at 18:05
  • ok thanks I will change the true and false in isjpg Mar 6, 2015 at 2:24
  • yes and the code will go on executing the lines below, but my problem is that isjpg always returns false and the loop never stoped executing Mar 6, 2015 at 8:46

You must log in to answer this question.

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