0

Need to figure out how to identify if I already have a JPG that I have found. So that if I have it will close the previous image and then start a new one.

My thought was that I need to check if my file pointer is NULL or not. So something like this...

if ( buffer[0] == 0xff && 
         buffer[1] == 0xd8 && 
         buffer[2] == 0xff && 
         ( buffer[3] & 0xf0 ) == 0xe0 )
    {
        // create file name and open the pointer
        sprintf(title, "%03i.jpg", i);
        FILE *img = fopen(title, "w");
        int pointer = ftell(img);
        // check to see if there is already a file open
        if ( img != NULL )
        {
            // close the previous file
            fclose(img);

            // open new file and write to buffer
        }

However, anytime I run the debugger on this as soon as it starts a file, it will always trigger that condition and then it fcloses.

Is my thinking right on how to check to see if we already have an image started? How would I check to make sure if there is a file open or not?

Thanks!

0

MARS is correct, but not quite complete in his answer. The code, as written, will always find an open file because it opens the file before checking.

Your thinking is correct, but not implemented. Simply put, the file pointer should be created earlier in the code and initialized to NULL. (It was never set to NULL in the posted code.) Then, it should be checked for NULL before opening the first file.

Then, there is a major scope issue. Note that creating the file pointer where its done now in the code will mean that it will cease to exist outside the if statement's code block.

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

0

Your approach is essentially correct, however the first thing to do when finding a new jpg is to close the previous if not NULL. The use of ftell () is totally unnecessary and I see no reason to use it in your code nor in the whole problem

0

You must log in to answer this question.

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