无论如何在C中的if条件之外使用文件指针

Is there anyway to use a file pointer outside of a if condition in C

我的问题是从 while 循环开始的。我有一个 if 条件,在 if 条件中我创建了一个文件并写入它。但自然地,我不能在条件之外使用指针。我是 C 的新手,我正在寻找一种方法来使该指针成为全局变量,如 python 中那样。这是我的代码:

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

//define chunk size
const int chunksize = 512;

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

    // open memory card file
    FILE *inptr = fopen(argv[1], "r");
    if (inptr == NULL)
    {
        fprintf(stderr, "Could not open %s.\n", argv[1]);
        return 2;
    }

    //create a variable to store memory size
    fseek(inptr, 0L, SEEK_END);
    long int memorysize = ftell(inptr);
    rewind(inptr);

    //find how many chunk does memory card contain
    int nofchunks = memorysize / chunksize;

    //create a file counter
    int nofjpeg = 0;

    while(nofchunks > 0)
    {
        //create a temporary storage
        unsigned char chunk[chunksize];

        // read a 512 byte chunk from memory card
        fread(&chunk, chunksize, 1, inptr);
        
        FILE *outptr = NULL;

        //check the chunk if it is a JPEG by looking first 4byte of the chunk
        if (chunk[0] == 0xff && chunk[1] == 0xd8 && chunk[2] == 0xff && (chunk[3] & 0xf0) == 0xe0)
        {
            nofjpeg++;

            //create a temporary file name
            char filename[8];

            if (nofjpeg == 1)
            {
                //create a new file name
                sprintf(filename, "%03i.jpg", nofjpeg);

                //open the file in write mode
                outptr = fopen(filename, "w");
                if (outptr == NULL)
                {
                    fprintf(stderr, "Could not open %s.\n", argv[1]);
                    return 2;
                }
                fwrite(&chunk, chunksize, 1, outptr);
            }
            else
            {
                //close the previous file
                fclose(outptr);

                //create a new file name
                sprintf(filename, "%03i.jpg", nofjpeg);

                //open the file in write mode
                outptr = fopen(filename, "w");
                if (outptr == NULL)
                {
                    fprintf(stderr, "Could not open %s.\n", argv[1]);
                    return 2;
                }
                fwrite(&chunk, chunksize, 1, outptr);
            }

        }
        else if(nofjpeg > 1)
        {
        fwrite(&chunk, chunksize, 1, outptr);
        }
        nofchunks--;
    }
}

您可以看到,在第一个内部 if 条件中,我打开了文件并写入了它。我需要在以下 else 条件下关闭该文件。您还可以看到我还在外部 if 条件之后使用了该指针。

if 语句之前声明文件指针,但为其分配一个 null 值。然后在 if 语句之后可用。在 if 语句中,您可以为其分配一个值,该值将在 if 语句之后保留。在 if 声明之后,您应该检查以确保在取消引用它之前它不是 null

//check the chunk if it is a JPEG by looking first 4byte of the chunk
FILE *outptr = (FILE*) NULL;   // Declare before the if and assign to NULL cast to type FILE*

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

    //create a temporary file name
    char filename[8];


    if (nofjpeg == 1)
    {
        //create a new file name
        sprintf(filename, "%03i.jpg", nofjpeg);

        //open the file in write mode
        outptr = fopen(filename, "w");   // Assign inside the if statement, but don't declare.
        if (outptr == NULL)
        {
            fprintf(stderr, "Could not open %s.\n", argv[1]);
            return 2;
        }
        fwrite(&chunk, chunksize, 1, outptr);
    }
    else
    {
        //close the previous file  ... this is likely a logical error; it should be NULL at this point, so there's nothing to close.
        if (outptr) fclose(outptr);   // Check to see if pointer is null before use.

        //create a new file name
        sprintf(filename, "%03i.jpg", nofjpeg);

        //open the file in write mode
        outptr = fopen(filename, "w");
        if (outptr == NULL)
        {
            fprintf(stderr, "Could not open %s.\n", argv[1]);
            return 2;
        }
        fwrite(&chunk, chunksize, 1, outptr);
    }

}
else if(nofjpeg > 1)
{
    // This is likely a logical error; outptr will be NULL here
    if (outptr) fwrite(&chunk, chunksize, 1, outptr);
}
nofchunks--;