0

So I just finished the code but it doesn't create any files and I can't figure out why. I'll be really grateful if any of you guys gives me a tip on what could be going wrong. The code:

/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */

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

#define BLOCK 512
typedef uint8_t BYTE;


int main(int argc, char* argv[])
{
    FILE* file = fopen("card.raw", "r");
    if(file == NULL)
    {
        printf("Could not open.\n");
        return 1;
    }

    BYTE buffer[BLOCK];
    int counter = 0;

    while(fread(&buffer, sizeof(BYTE), BLOCK, file) == BLOCK)
    {
        if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
        {
            char store[8];
            sprintf(store, "%03d.jpg", counter);

            FILE* img = fopen(store, "a");
            if(img == NULL)
            {
                printf("Could not create file.");
                return 2;
            }

            fwrite(&buffer, sizeof(BYTE), BLOCK, img);

            counter++;

            while(fread(&buffer, sizeof(BYTE), BLOCK, file) == BLOCK)
            {
                if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] == 0xe0 || buffer[3] == 0xe1))
                {
                    fclose(img);

                    sprintf(store, "%03d.jpg", counter);

                    img = fopen(store, "a");
                    if(img == NULL)
                    {
                        printf("Could not create file.");
                        return 2;
                    }

                    fwrite(&buffer, sizeof(BYTE), BLOCK, img);

                    counter++;
                }
                fwrite(&buffer, sizeof(BYTE), BLOCK, img);
            }
            fclose(img);
        }
    }
    fclose(file);
    return 0;
}
1

For starters, it still has the same problem as the deleted question: char* store[8]; is an array of pointers, not a char array.

Moving past that, look at the following:

FILE* img = fopen("store", "a");

When you put double quotes around something, it becomes a literal, not a variable. This line opens a file called "store" because it's using the literal string. If you do a directory listing, you will probably find it in the same directory. Without the quotes, it would use the name stored in the variable store. As it is, it is just opening and closing the file named "store" over and over and appending to it. BTW, what happens when a file already exists and you open it for "a" or append, vs. "w" or write?

There are more problems waiting, but those will be for another question.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • I fixed the char* one, but it appeared in my new post because I rushed and copied it from the last post. I'm checking the rest now. – Petar Petrov Aug 29 '16 at 2:25
  • Okay, so now only the first jpg (000) opens. Any tips on that? I thought over what you said about the "w" vs "a" but can't seem to find the answer there. I editted the post so now the code is changed. – Petar Petrov Aug 29 '16 at 2:35
  • Yes, my thoughts are that you should work on it for a while and study what the code is doing vs. what you want it to do. Some strategically placed printf statements or running it through GDB might also assist you. You need experience debugging. This will be the single most important skill you can develop as a programmer, so don't give up so soon! – Cliff B Aug 29 '16 at 2:48

You must log in to answer this question.

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