0

Hi I'm working on pset4 recoverand I'm having trouble understanding something. My code does not work like it's suposed to, in fact it only creates 1 file "000.jpg" and that one isn't even readable. However it compiles fine. The issue I ran into when compiling was using the file pointers throughout the diffrent if-statements. I followed along with what the error messages told me to do and came up with the code below. The relevant lines of code are 41 and 50 (*output = *fopen(filename,"w");). I don't fully understand why this would work (as opposed to *output = fopen(filename,"w");) and I'm also not exactly sure what it does. I know its called derefrencing the pointer, but I'm unsure if that is what I should be doing here. My understanding of the concept is vague at best so I would appreicate it if someone could enlighten me of the specifics and possibly provide a few hints in regards to what I could do instead.

    int buffer[sizeof(char) * 512];
    bool end = false;
    int img_counter = 0;

    while (end == false)
    {
        char filename[7];
        sprintf(filename, "%03i.jpg", img_counter);
        FILE *output = fopen(filename, "w");

        fread(buffer,1,512,card);

        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {

            if (img_counter == 0)
            {
                fclose(output);
                sprintf(filename, "%03i.jpg", img_counter);
                *output = *fopen(filename,"w");
                fwrite(buffer,1,512,output);
                img_counter++;
            }

            if (img_counter != 0)
            {
                fclose(output);
                img_counter++;
                *output = *fopen(filename,"w");
                sprintf(filename, "%03i.jpg", img_counter);
                fwrite(buffer,1,512,output);
            }

        }
        else
        {
            fwrite(buffer,1,512,output);
        }

        if (fread(buffer,1,512,card) < 512)
        {
            end = true;
            return 0;
        }
    }
}

You must log in to answer this question.

Browse other questions tagged .