0

I'm not sure what I'm doing wrong. I tried to follow the walkthrough. My code is below. Any help will be appreciated.

typedef uint8_t BYTE; typedef struct { BYTE first; BYTE second; BYTE third; BYTE fourth; }SIGNATURE;

int main(int argc, char* argv[]) {

// USE BUFFER? // FIGURE OUT SPRINTF // START JPEG LABEL AT 0

// read in chunks of 512 bytes
// compare to 0xff 0xd8 0xff 0xe0  || 0xff 0xd8 0xff 0xe1
// if matched, start new file
// if doesn't, continue old file
// name file using sprintf
// when ends, out as a jpg file


// compare and find
// if matched, start new file
// need char for sprint f (8 as needs null terminator)
char name [8];
FILE* file = fopen("card.raw", "r");

// open first output file

// str name = sprintf(name, "%3d.jpg", 0); FILE* outptr = fopen(name, "w"); if (outptr == NULL)

{
    fclose(file);
    fprintf(stderr, "Could not create %s.\n", name);
    return 3;
}

    // temp storage
    SIGNATURE sig;
    // read signature from infile
    fread(&sig, sizeof(SIGNATURE), 1, file);

    // compare to 0xff 0xd8 0xff 0xe0  || 0xff 0xd8 0xff 0xe1
    if (sig.first == 0xff && sig.second == 0xd8 && sig.third == 0xff && (sig.fourth==0xe0 || sig.fourth == 0xe1))
    {
        fwrite(&sig, 1, sizeof(SIGNATURE), outptr);

        fread(&sig, 1, 512 - sizeof(SIGNATURE), file);
        fwrite(&sig, 1, 512 - sizeof(SIGNATURE), outptr);

        int file_name = 0;
         while (!feof(file))   // while (fgets(output, sizeof(output), file) != NULL)
         {
            //read in signature
            fread(&sig, sizeof(SIGNATURE), 1, file);
            //if not equal to, keep reading and writing 
            if (!(sig.first == 0xff && sig.second == 0xd8 && sig.third == 0xff && (sig.fourth==0xe0 || sig.fourth == 0xe1)))

            {
            // write the signature
            fwrite(&sig, 1, sizeof(SIGNATURE), outptr);
            // read and then write the remaining bytes in 512 block
            fread(&sig, 1, 512 - sizeof(SIGNATURE), file);
            fwrite(&sig, 1, 512 - sizeof(SIGNATURE), outptr);

            }

            else
            {

                // close the first file

                fclose(outptr);
                file_name++;
                 char title [8];
                // convert file name to char
               sprintf(title, "%3d.jpg", file_name);

               // open new file
               FILE* outptr = fopen(title, "w");

            // write the signature
            fwrite(&sig, 1, sizeof(SIGNATURE), outptr);
            // read and then write the remaining bytes in 512 block
            fread(&sig, 1, 512 - sizeof(SIGNATURE), file);
            fwrite(&sig, 1, 512 - sizeof(SIGNATURE), outptr);                    

            }

    }
}



// close infile
fclose(file);

// close outfile
fclose(outptr);

}

1 Answer 1

0

First, a quick way to format code properly: select the pasted code, then click the {} icon in the format bar. You can do that when you create a post, or edit your post now and fix the formatting.

Next, the program.

Read 4 bytes into sig here fread(&sig, sizeof(SIGNATURE), 1, file);. If it's not a jpg signature? Close files, exit program, done. From the problem description it is unlikely that the first thing in card.raw is a jpg.

Warning, the "4 byte method" looking for the jpg sig is fraught. It usually doesn't handle finding a jpg signature in the middle of the "junk" part at the beginning of card.raw. The authors of check50 are pretty savvy and such approaches invariably fail because of it. FWIW.

Also, make sure you are using the current spec. It describes the fourth byte of the jpg sig thus:

The fourth byte, meanwhile, is either 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, of 0xef. Put another way, the fourth byte’s first four bits are 1110.

You must log in to answer this question.

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