0

I'm almost there, but one more question about recover..

In my code, I open a file inside an if condition, but how do I acces that file later? It's getting out of scope and I'm getting use of undeclared identifier 'img' errors.

while(...)

if (found beginning of jpeg)
     if(already opened a jpeg before)
        close the previous file

     open new file and append to it 
     FILE* img = fopen("001.jpg, "a")

else
    just append to already opened file 
    fwrite(&buffer, sizeof(BYTE), 512, img) (but the file I created before is now out of scope..)

1 Answer 1

0

You declare img in the if block, and it only actually exists there. The else block has no idea what img is (and in fact, you can create another variable called img and set it to something else).

To be able to access it in both places, you must declare img outside (well, above) the if ... else block. Then in the if block, just set img = fopen("001.jpg, "a").

while(...)

FILE* img

if (found beginning of jpeg)
     if(already opened a jpeg before)
        close the previous file

     open new file and append to it 
     img = fopen("001.jpg, "a")

else
    just append to already opened file 
    fwrite(&buffer, sizeof(BYTE), 512, img)
2
  • Sorry to break the rules, but Thank you, AirThomas! I was struggling with this for 2 hours with absolutely everything else correct. I failed to notice that I had already initialized the outfile as a global variable up top and I was reinitializing with FILE* again instead of just assigning it. This problem set was by far the most difficult so far. Thanks again!
    – user1661
    Commented Oct 10, 2014 at 4:51
  • also for FILE* img, you might also want to initialise it to FILE* img = NULL; at first Commented Feb 27, 2015 at 5:14

You must log in to answer this question.

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