0

I am working on pset4's recover. My variable, outptr, is not being recognized by other parts of my do-while loop. Why is this?

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

   #include "jpeg.h"

   int main(int argc, char* argv[])
{
if (argc != 2)
{
    fprintf(stderr, "Usage: ./recover image");
    return 1;
}

//Remember filenames
char *infile = argv[1];

// Open file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
    fprintf(stderr, "Could not open %s. \n", infile);
    return 2;
}
//Declaring constants and variables
int eof = 0;
BYTE buffer[512];
int counter = -1;
char image[4];
int IMAGENO = 0;

do
{   
    //Reading into array
    fread(&buffer, 512, 1, inptr);

    //Checking if EOF has been reached
    if (fread(&buffer, 512, 1, inptr) == 0)
        eof++;

    sprintf(image, "%03i.jpg", IMAGENO);
    FILE *outptr = fopen(image, "w");

    //Checking if JPEG header
    if (buffer[0] == 0xff &&
    buffer[1] == 0xd8 &&
    buffer[2] == 0xff &&
    (buffer[3] & 0xf0) == 0xe0)
    {
        counter++;
        if (counter == 0)
        {
            /*Start first JPEG*/
            sprintf(image, "%03i.jpg", IMAGENO);
            IMAGENO++;
            FILE *outptr = fopen(image, "w");
            fwrite(inptr, 512, 1, outptr);
        }
        else
        {
            fclose(outptr); /* Old JPEG*/
            /*Start next JPEG*/
            sprintf(image, "%03i.jpg", IMAGENO);
            IMAGENO++;
            FILE *outptr = fopen(image, "w");
            fwrite(inptr, 512, 1, outptr);
        }

    } 
    else
    {
        if (counter > -1)
        {
            /*Bytes belong to currently opened JPEG*/
            fwrite(inptr, 512, 1, outptr);
        }
        //else
        //{
            /* Discard 512 bytes and start loop again*/;
        //}
    }

}
while (eof == 0);

fclose(outptr);
fclose(inptr);

}

1 Answer 1

2

It's a scope issue. You have declared outptr inside of a pair of curly braces - the curly braces that contain the code for the if statement's code block. As soon as the code exits that set of curly braces, the var outptr ceases to exist.

If you had declared it before starting the do loop, it would persist through the rest of the program because the scope would be contained by the curly braces for main(). In other words, it would persist from the declaration to the end of the program. Note that declaring and initializing are two different steps.

Here's a pseudocode graphic that might help explain:

int main(void)
{
    int aaa;
    // aaa now exists and is in scope
    {    
        int bbb;
        //  bbb now exists and both aaa and bbb are in scope

        {
            int ccc;
            // ccc now exists and aaa, bbb and ccc are now in scope
        }

        //  ccc is out of scope and no longer exists
        //  aaa and bbb are still in scope
        {
            int ddd;
            // ddd now exists and is in scope
            // aaa and bbb are still in scope
        }

        // ddd is out of scope and no longer exists
        //  aaa and bbb are still in scope
    }

    // bbb is now out of scope and no longer exists
    // aaa is the only variable that still persists and is still in scope
}

// program has exited, nothing with scope inside of main() still exists.
// If there were global vars (later lessons) 
// they would persist into functions that would follow. 
// Any vars inside of a function would follow the same scope rules 
// that are demonstrated above.

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

5
  • Then why does my do-while terminate when the variable eof changes, the change in eof being inside the confines of an if statement?
    – Jason_V
    Commented Apr 24, 2017 at 21:49
  • Look at where the var eof is declared. The scope is bound by the curly brace pair that it is inside of - the opening curly brace that follows the main() statement and the closing curly brace that defines the end of main(). That means it is in scope from the point where it is created until the end of the program. Scope doesn't end when a new opening curly brace is encountered, it is defined by the pair that immediately surrounds the variable, even though that pair may surround the entire program, or even if there are more pairs that surround a subset of the program, like a while loop.
    – Cliff B
    Commented Apr 24, 2017 at 23:14
  • Thanks for the help. I fixed my code and ironed out some bugs, but it still doesn't open the files! Can you help me identify an error? The code is above:
    – Jason_V
    Commented Apr 25, 2017 at 5:06
  • No. But if you'll roll back the edit to preserve the original question, and create a new question (it's a new problem you need help with) along with explanation, that's a different matter. ;-)
    – Cliff B
    Commented Apr 25, 2017 at 6:03
  • Ok, I reversed my edit. I also finally figured out my problem. The code works perfectly now! :D Thanks for your help on my previous question.
    – Jason_V
    Commented Apr 25, 2017 at 12:33

You must log in to answer this question.

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