//initialise variables
FILE* file = fopen("card.raw", "r");
int buffer[512];
int jpgsig1[] = {0xff, 0xd8, 0xff, 0xe0};
int jpgsig2[] = {0xff ,0xd8 ,0xff ,0xe1};
int jpgsig3[] = {0x00 ,0x00 ,0x00 ,0x00};
int imgcounter = 1;
char title[10];
FILE* jpg;
int togglewrite = 0;
int toggleopen = 0;

//open file and read through it 
while(fread(&buffer, 512, 1, file)==1){
    if((memcmp(buffer, jpgsig3, 4) == 0)){  //check if memcmp works for zeros
    if((memcmp(buffer, jpgsig1, 4) == 0) || (memcmp(buffer, jpgsig2, 4) == 0)){  //doesnt work
        if(toggleopen == 1){

Hello, i have some Problems with pset4. I don't find any signatures of the .jpg and i don't now why. memcmp works for 0x00 but not for the jpg signatures. does anyone have a hint want i'm doing wrong? :(

ps: in the previous file they always used & infront of the variable like in fread(&buffer, 512, 1, file)==1)´ why? it seems to work also without it...

kind regards


ok i found the problem. i have to use uint8_t as datatype instead of int. i don't really get why though.


Since your problem is now resolved I'll instead use this answer to explain why what you see happens, and also what memcmp does.

I don't understand why uint8_t works but not int

uint8_t is guaranteed to be an unsigned integer type that's exactly 8 bits wide. It usually based on an unsigned char but since implementations vary, so will this.

I am not exactly sure why int is being rejected (the error message would help). However uint8_t is just big enough to store a two digit hexadecimal value, thus an int would be a lot of wasted space, it still shouldn't get rejected though.

What is memcmp and how do I use it?

The prototype for memcmp is the following:

int memcmp(const void *s1, const void *s2, size_t n);

Pay attention to the * as these denote a pointer, that is, memcmp takes two pointers as arguments. However since buffer is a pointer to buffer[0] prefixing the the array with an & is not required.

And the description is:

The memcmp() function compares the first n bytes (each interpreted as unsigned char) of the memory areas s1 and s2.

Quote "each interpreted as unsigned char" tells us that memcmp casts our int to an unsigned char or uint8_t before it compares the two, the typecast could cause some issues.

Return value:

The memcmp() function returns an integer less than, equal to, or greater than zero if the first n bytes of s1 is found, respectively, to be less than, to match, or be greater than the first n bytes of s2.

From all this, we now how to use memcmp and what it does, here's a translation of
if (memcmp(buffer, jpgsig1, 4) == 0) (skipping the latter part for brevity):


If this answers your question please accept the answer by clicking the check mark to remove this question from the unanswered pool, otherwise feel free to leave a comment ;)

  • 1
    int doesn't work because it takes the single byte and stores it in a 4 byte int. In the process, the remaining 3 bytes are padded out using twos complement notation. Since the single byte is the equivalent of a negative number in twos complement, the 3 bytes are padded out as ff ff ff. Now, what happens when you try to compare that to the 4 bytes of a signature? That's why unsigned vars are so important here, as well as a single byte type.
    – Cliff B
    Nov 23 '16 at 21:35

You must log in to answer this question.

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