fread returns 与 nmemb 相同的数字但已到达文件末尾

fread returns same number as nmemb yet end of file is reached

我正在 CS50 中完成一个问题,我的代码是成功的,尽管我不理解其中的测试行为。 第 63 行 if (feof(inptr)) 检查是否到达文件末尾,然后我要求打印缓冲区指针的大小,该指针应小于初始化为 (512) 的大小。 尽管达到了 EOF,它仍然 returns 值为 512,这没有意义。

谁能告诉我哪里出了问题?

#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;
}

sizeof(buffer) 告诉你 buffer 有多大。它不会告诉您有关其中内容的任何信息——没有告诉您当前有效的字节数,也没有告诉您最近读取的字节数 fread.

了解fread 读取了多少字节的正确方法是使用它的return 值。您应该使用如下代码:

size_t BytesRead = fread(buffer, 1, sizeof(buffer), inptr);

如果在这条语句之后,BytesRead 小于 sizeof buffer,那么 fread 没有读取您要求的所有字节。这表明出现了问题,并且比是否设置了文件 EOF 标志更好。

(sizeof) returns size in bytes of the object representation of type

https://en.cppreference.com/w/cpp/language/sizeof

由于您为 buffer 分配了 512 个字节,sizeof 将 return 512。保存什么缓冲区并不重要,因为它不是长度检查。