I am completing a problem in CS50, and my code is successful although I don't understand the behavior of a test inside of it.
Line 63 if (feof(inptr))checks if the end of file is reached, and then I ask to print the size of a buffer pointer which should be less than what it was initialized to (512).
It still returns a value of 512 although the EOF is reached, which doesn't make sense.
Could someone tell me what is wrong?
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
    // ensure proper usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: copy infile outfile\n");
        return 1;
    }
    // remember filenames
    char *infile = argv[1];
    char *outfile = "000.jpg";
    // open input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }
    // open output file
    FILE *outptr = fopen(outfile, "w");
    if (outptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not create %s.\n", outfile);
        return 3;
    }
    // declaring variable
    unsigned char buffer[512];
    int count = 0;
    int test = 512;
    // Execute until we find end of card
    while (!feof(inptr))
    {
        // Read buffer in card
        fread(buffer, 1, sizeof(buffer), inptr);
        // Checks for jpeg signature
        if (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] & 0xf0) == 0xe0)
        {
            fwrite(buffer, 1, sizeof(buffer), outptr);
            fread(buffer, 1, sizeof(buffer), inptr);
            // Checks if we are still in a jpeg, not the beginning of new one
            while (buffer[0] != 0xff ||
            buffer[1] != 0xd8 ||
            buffer[2] != 0xff ||
            (buffer[3] & 0xf0) != 0xe0)
            {
                // Exits loop if end of file
                if (feof(inptr))
                {
                    int size = sizeof(buffer);
                    printf("%i\n", size);
                    break;
                }
                fwrite(buffer, 1, sizeof(buffer), outptr);
                fread(buffer, 1, sizeof(buffer), inptr);
            }
            if (feof(inptr))
            {
                break;
            }
            // Close jpeg
            fclose(outptr);
            // Change count to apply to next jpeg title
            count++;
            char img_num[4];
            sprintf(img_num, "%03i.jpg", count);
            // Assign new title to new jpeg
            outfile = img_num;
            printf("%s\n", outfile);
            outptr = fopen(outfile, "w");
            // We will have to read again in the main loop, so rewind
            fseek(inptr, -512, SEEK_CUR);
        }
    }
    printf("%i\n", test);
    // close infile
    fclose(inptr);
    // close outfile
    fclose(outptr);
    // success
    return 0;
}
 
     
     
    