0

i am really confused as to why i am getting 0 in my gradebook for recovery. It recovers all the photos and returns check50 with green smiley faces. I don't know what to fix it there is no problems reported and program recovers the photos successfully. I am posting my code below. Would greatly appreciate any tips as what should i look into. thank you!

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

    if (argc > 1)
    {
        printf ("No argument necessary");
        return 1;
    }

    FILE* mem_card = fopen("card.raw","r");    
    if (mem_card == NULL)
    {
        return 2;
    }
    unsigned char buffer [512];
    fseek(mem_card, 0, SEEK_END);
    int file_length = ftell(mem_card);
    int times = file_length / 512;
    fseek(mem_card, 0, SEEK_SET);
    int counter = 0;
    FILE * output = NULL;
    char* title [16]= {"000.jpg","001.jpg", "002.jpg","003.jpg", "004.jpg","005.jpg", "006.jpg","007.jpg", "008.jpg","009.jpg", "010.jpg","011.jpg", "012.jpg","013.jpg", "014.jpg","015.jpg"};
    int no_jpg = 0;
    while(counter < times)
    {
        for (int i = 0; i < 512; i++)
        { 
            if (feof(mem_card))
            {
                fclose(mem_card);
                return 0;
            }

            fread (&buffer[i], 1, 1, mem_card);

        }
        counter++;

        if (buffer[0] == 0xff && (buffer[1] == 0xd8) && (buffer[2] == 0xff) && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            if (output == NULL) 
            {
                output = fopen(title[no_jpg], "w");
                if (output == NULL) 
                {
                    return 1;
                }
                no_jpg++;
                fwrite(&buffer, 512, 1, output);
            }
            else if (output != NULL)
            {
                fclose(output);
                fopen(title[no_jpg], "w");
                if (output == NULL) 
                {
                    return 1;
                }
                no_jpg++;
                fwrite(&buffer, 512, 1, output);
            }
        }
        else if (output != NULL)
        {
            fwrite(&buffer, 512, 1, output);
        } 
    }
    fclose(output);
    fclose(mem_card);
}
1
  • ive been told, the code is too slow because i read 1 byte at the time and I shouldnt have hard coded the files names, I will work on it now, but if anyone has additional input, I would apreciate it!
    – Mags
    Commented Aug 31, 2015 at 17:54

1 Answer 1

1

Well since your code really does recover all the photos correctly, and it even passes check50 I would say that your code was also checked by a human, and since you have hard-coded the name files, and you don't follow the instructions by the specification (i.e. reading 512 bytes at a time), the graded your code as 0. Fix those problems and try again.


If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark. And don't forget to keep coding!

1
  • yeah i know about the 512, but whenever i change 1 do 512 i have Segmentation fault (core dumped) trouble, programming is so confusing :) thanks for your input.
    – Mags
    Commented Sep 1, 2015 at 15:22

You must log in to answer this question.

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