0

I'm stuck. Use of undeclared identifiers img and filename... I dont get it. Something with the pointers? Could anybody give me some tips to get further. Thanks!

My code:

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

unsigned char buffer[512]; //create buffer space to save read part of 512 bytes by fread
int firstfound = 0; //set 0 for not found yet
int num_found = 0; //how many jpg found
char startcheck1[4] = {0xff, 0xd8, 0xff, 0xe0}; //posible start of jpg
char startcheck2[4] = {0xff, 0xd8, 0xff, 0xe1}; //posible start of jpg

int main()
{
    FILE * inptr = fopen("card.raw", "r"); //create infile pointer

    if(fread(&buffer, sizeof(char), 512, inptr) == 512) //as long as we can still read 512 bytes
    {
        fread(&buffer, sizeof(char), 512, inptr); // read theam 512 bytes
        if (buffer[0] == startcheck1[0] && buffer[1] == startcheck1[1] && buffer[2] == startcheck1[2] 
            && (buffer[3] == startcheck1[3] || buffer[3] == startcheck2[3])) // if beginning of jpg
        {
            fclose(img); // close current file
            num_found++; //increment number found by 1
            firstfound = 1; // make true
            sprintf(filename, "00%d", (num_found - 1)); //prints new name to filename
            FILE * img = fopen(filename, "a"); // create file pointer that points to filename and opens that file pointed to
            fwrite(&buffer, sizeof(char), 512, img); // write buffer to file that file pointer points to
        }
        else
        {
            if(firstfound == 1) //if first start already found
            {
                fwrite(&buffer, sizeof(char), 512, img); //write buffer
            }
        }
    }

    fclose(img);
    return 0;
}

1 Answer 1

1

Take a close look at where you are declaring img.

if(....)
{
        fclose(img); // close current file
        num_found++; //increment number found by 1
        firstfound = 1; // make true
        sprintf(filename, "00%d", (num_found - 1));
        FILE * img = fopen(filename, "a"); 
        fwrite(&buffer, sizeof(char), 512, img); 
} 
....
fclose(img);  

The statement that actually creates the var img is FILE * img = fopen(filename, "a");. But, you are closing a file that is supposed to be pointed to by img 4 lines earlier! That's the line that is triggering the error. You are using the var img before you declare it.

You have another issue with img. The line fclose(img) will throw an error as it is written because it no longer exists at that point. It is declared inside the brackets of the "if" statement, so it will be out of scope at the end of the file.

Try declaring the file pointer img at the top of the main block, maybe setting it to NULL to start.

Thefilename error is straightforward. It simply isn't declared anywhere. You might try declaring it right after declaring img, maybe as a character string?

That should get you going in the right direction.

If this answers your question, please mark this question as answered. Let's keep up on forum housekeeping. ;-)

2
  • Hi Cliff B, thanks for your help :) I think it answers my question. And i did exactly tried this, but when i do, compiling the file seems to go fine but when i run in i get a segmentation dump. Any clue why that would happen? Commented Apr 24, 2015 at 18:23
  • That's an entirely new question that depends on the new code and the error message and knowing what line is causing it. Generally though, a segmentation fault occurs when you think you're accessing something specific, but a pointer var is actually going off somewhere it shouldn't. Happens a lot with pointers. Step through your program using gdb and figure out which line is causing the fault and then look at everything in that line. Try to figure out exactly what component in the line is bad. If you have to, put a printf() one line earlier and try to print out each component, 1 per line.
    – Cliff B
    Commented Apr 24, 2015 at 20:39

You must log in to answer this question.

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