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

    // I got information about what to do with the buffer
    // from this stack exchangehttps://cs50.stackexchange.com/questions/2680/
    //what-data-structure-do-you-use-to-store-the-buffer-in-recover

    //typedef uint8_t  BYTE;
            //{
                //BYTE buffer;
            //}

    int main(int argc, char *argv[])
    {
        int filenumber;
        // ensure proper usage
        if (argc != 2)
        {
            fprintf(stderr, "You didn't enter two command line arguments\n");
            return 1;
        }

        char *infile = argv[1];

        // open input file
        FILE *inptr = fopen(infile, "r");
        if (inptr == NULL)
        {
            fprintf(stderr, "Could not open %s.\n", infile);
            return 2;
        }

        int *card = malloc(512);

        if (fread(&card,1,512,inptr) == 512)
        {
        // would I then do something here about freading into the buffer
        // what I just fread into card. I don't understand why I can't just
        // access each part of card like an array


            if (card[0] == 0xff &&
                card[1] == 0xd8 &&
                card[2] == 0xff &&
                (card[3] & 0xf0) == 0xe0)
                {
                    filenumber++;

                    if (filenumber == 1)
                    {

                        char* outfile;

                        sprintf(outfile, "%i.jpg", filenumber);

                        FILE *outptr = fopen(outfile, "w");
                        if (outptr == NULL)
                        {
                            fclose(inptr);
                            fprintf(stderr, "Could not create %s.\n", outfile);
                            return 3;
                        }

                        fwrite(&card,1,512, outptr);
                        // i'm going to need to open *outptr before this parenthese
                    }

                    else if (filenumber > 1)
                    {
                        fclose (outptr);

                        char* outfile;

                        sprintf(outfile, "%i.jpg", filenumber);

                        FILE *outptr = fopen(outfile, "w");
                        if (outptr == NULL)
                        {
                            fclose(inptr);
                            fprintf(stderr, "Could not create %s.\n", outfile);
                            return 3;
                        }
                    }
                }
        }

        else if (card[0] ==! 0xff) //&&
            //card[1] =! 0xd8 &&
            //card[2] =! 0xff &&
            //(card[3] & 0xf0) =! 0xe0)
        {

            if (filenumber > 0)
            {
                fwrite(&card,1,512, outptr);
            }
            //else if filenumber is 0 just do nothing, as I am doing
        }
        else
        {
            fclose (inptr);
            fclose (outptr);
        }
    }

    These are the error codes I got
    recover.c:70:37: error: use of undeclared identifier 'outptr'
                        fclose (outptr);
                                ^
    recover.c:95:41: error: use of undeclared identifier 'outptr'
                fwrite(&card,1,512, outptr);
                                    ^
    recover.c:102:25: error: use of undeclared identifier 'outptr'
            fclose (outptr);

My question is why my codes doesn't recognize outptr

2
  • Is there a question here? – Cliff B Aug 16 '18 at 0:00
  • Yes, sorry if I didn't do a good job of making it clear initially – user21860 Aug 16 '18 at 0:05
1

The error message is clear: You did not declare outptr. Or at least where you use it, it is not declared. Which brings us to the topic of "scopes".

In C, variables are block-scope, they exist in the block they are declared in (and are visible also in inner blocks), but after leaving the block, their memory is declared unused. That's done by resetting the stack pointer to where it was before entering the block. The next variable declared will re-use that memory.

A block in this sense is usually marked with {} (where it can be used to group statements), but also a for loop's head has its own scope, variables declared there aren't visible to code outside the loop.

This means you need a FILE *outptr; or maybe better FILE *outptr = NULL; further up in your main function, and avoid declaring the variable in an inner code block.

Another thing: Use card, not &card. &card is the memory address of the variable, not the memory address of the area it is pointing to.

1
  • Thanks for helping, my code compiles now but unfortunately when I try and run it doesn't do anything, so I posted a new question – user21860 Aug 16 '18 at 19:35

You must log in to answer this question.

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