what is the meaning of

fseek(file, -4, SEEK_CUR);

Recommended Answers

All 4 Replies

It moves the file pointer 4 bytes towards the beginning of the file from the current position.

can you explain why this code always enter an infinte loop card.raw is a file containg jpg pic that i an trying to recover.

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

int createFile(FILE* file, FILE* outfile);
int createName(void);

int main (void)
{

    FILE* cardp = fopen("card.raw", "r");
    FILE* outp = fopen("1.jpg", "w");

    while (!feof(cardp))
    {
        if (((fgetc(cardp) == 0xff) && (fgetc(cardp) == 0xd8) && (fgetc(cardp) == 0xff) && (fgetc(cardp) == 0xe0)) || ((fgetc(cardp) == 0xff) && (fgetc(cardp) == 0xd8) && (fgetc(cardp) == 0xff) && (fgetc(cardp) == 0xe1)))
        {
            printf("here");

            createFile(cardp, outp);
        }
    }   
    return 0;
}

int createName(void)
{

    return 0;
}

int createFile(FILE* file, FILE* outfile)
{

    //while not at the bening of another file do:
    while (1)
    {
        char block[512];
        fseek(file, -4, SEEK_CUR);
        fread(&block, 512, 1, file);

        //write 512 block
        fwrite(&block,512, 1, outfile);

        if (((fgetc(file) == 0xff) && (fgetc(file) == 0xd8) && (fgetc(file) == 0xff) && (fgetc(file) == 0xe0)) || ((fgetc(file) == 0xff) && (fgetc(file) == 0xd8) && (fgetc(file) == 0xff) && (fgetc(file) == 0xe1)))
            break;


    }

    return 0;
}

Did you use your compiler's debugger so that you can see exactly what is happening? I can't answer your question without having a copy of card.raw and debugging your program.

If it reads 512 bytes, then backs up 4 bytes, what makes you think that the next 8 bytes will be the same as what that if statement expects? Is the size of the file evenly divisible by 512?

thanks dragon. you helped me. i should have check every 512 bytes i read to see if it is off the patten not only 4 thanks a lot

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.