CS50恢复运行并恢复除第一个文件外的所有文件

时间:2019-04-09 13:55:45

标签: cs50

我的问题是有关CS50的恢复任务。

该代码正在运行(最终),它产生50个JPEG文件,并且大多数都是正确的图像,除了第一个文件不是图像,因此它不通过check50。

我花了很长时间试图找出问题所在,但是我无法查明问题所在,因此我希望有人可以帮助我,以便我继续前进。

提前谢谢!这是我的代码:

    int main(int argc, char *argv[])
    {
    // ensure proper usage
    if (argc != 2)
    {
        fprintf(stderr, "Usage: Name of Memory Card File\n");
        return 1;
    }

    char *readfile = argv[1];

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

    //Declare a buffer to read into
    unsigned char *buffer = malloc(512);

    //to check if we have already found a file
    bool (jpgAlreadyNew) = false;

    //declare counter for the number of files found and a file pointer
    int filenumber = 0;
    FILE *new_jpg_ptr = NULL;
    char filename[8];

    //read in bytes until reach EOF
    while (fread(buffer, 1, 512, card_ptr) != 0x00)
    {
        //if we reach the header pattern of bytes
        if (buffer [0] == 0xff && buffer [1] == 0xd8 && buffer [2] == 0xff && (buffer [3] & 0xf0) == 0xe0)
        {

            //if there is not already a JPEG file found
            if (!jpgAlreadyNew)
            {
                //change the bool value
                (jpgAlreadyNew) = true;

                //open new file
                sprintf(filename, "%03i.jpg", filenumber);
                new_jpg_ptr = fopen(filename, "w");
                if (new_jpg_ptr == NULL)
                    {
                        return 3;
                    }
                //add to counter of files found
                filenumber++;

                //write files from buffer into new img file
                fwrite(buffer, 1, 512, new_jpg_ptr);

            }

            //if there is already a JPEG file found
            if (jpgAlreadyNew)

            {
                //close the previous file which would now be complete
                fclose(new_jpg_ptr);

                //open new file
                sprintf(filename, "%03i.jpg", filenumber);
                new_jpg_ptr = fopen(filename, "w");
                if (new_jpg_ptr == NULL)
                    {
                        return 4;
                    }
                //add to counter of files found
                filenumber++;

                //write files from buffer into new img file
                fwrite(buffer, 1, 512, new_jpg_ptr);

            }

        }

        // else if we do not see pattern of header bytes
        else
        {
            //if already found a jpg file which is open then write the bytes to that file
            if (jpgAlreadyNew)
              {
                  fwrite(buffer, 1, 512, new_jpg_ptr);
              }
            //if no file found yet, discard and move on
            if (!jpgAlreadyNew)
              {
                  continue;
              }
        }
    }

    //free memory
    free (buffer);

    //close pointers and end program successfully
    fclose(card_ptr);
    fclose(new_jpg_ptr);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

让我们从查找第一个jpeg签名开始遍历该程序:

authors的计算结果为true,因此进入if块;它要做的第一件事是if (!jpgAlreadyNew)。完成创建文件并写入第一个块后,接下来会发生什么?此(jpgAlreadyNew) = true;。哪个评估为true。因此它将关闭000.jpg并继续前进。

由于if (jpgAlreadyNew)是布尔值,因此jpgAlreadyNew构造就足够了。