0
/**
 * recover.c
 *
 * Computer Science 50
 * Problem Set 4
 *
 * Recovers JPEGs from a forensic image.
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main(void)
{
    FILE* file = fopen("card.raw", "r");
    if (file == NULL)
    {
        return 1;
    }

    for (int i = 0; i < 51; i++)
    {
        sprintf(title, "00%d.jpg", i);
        FILE* img = fopen(title, "a");
        int* buffer = malloc(sizeof(uint8_t) * 512);

        if(fread(&buffer, 512, 1, file) == 0xffd8ffe)
        {
            fwrite(&buffer, 512, 1, file);
            fclose(file);
            free(buffer);
        }
    }
}

I'm getting:

recover.c:23:17: error: use of undeclared identifier 'title'; did you mean 'file'?
        sprintf(title, "00%d.jpg", i);
                ^~~~~
                file
recover.c:15:11: note: 'file' declared here
    FILE* file = fopen("card.raw", "r");
          ^
recover.c:24:27: error: use of undeclared identifier 'title'; did you mean 'file'?
        FILE* img = fopen(title, "a");
                          ^~~~~
                          file
recover.c:15:11: note: 'file' declared here
    FILE* file = fopen("card.raw", "r");
          ^
2 errors generated.

1 Answer 1

0

Have you ensured title is within the scope of main()? It is quite possible you only have it available within the function that previously declared it.

Update: I recommend you compile and see for yourself, although assuming it is above the first for loop inside main, then yes. However the title should be unique, three digits long (padded if necessary) and initially zero; I recommend you create a function for that, so you can call
char* title = get_filename();

where get_filename looks something like:

int count = 0;
char* get_filename()
{
    // Dostuff
    count++;
}

Alternatively you can also use static variables, which "remember" their state between function invocations.

2
  • I added char* title = "title"; before the start of the loop. Did I fix the compiling problem correctly?
    – Cristian
    Commented Nov 22, 2016 at 18:23
  • See my updated answer. Commented Nov 22, 2016 at 18:37

You must log in to answer this question.

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