cs50:pset 4:恢复。 Check50给出以下错误:'预期退出代码0,而不是1'

时间:2017-10-25 07:59:42

标签: c cs50 recover

我已完成任务并可以查看.jpg图像,但是当我运行check50时,我收到以下错误消息:

:) recover.c exists.
:( recover.c compiles.
expected exit code 0, not 1
...etc. 

当我在终端中运行echo $?时,我得到的值为零,所以这似乎并不是指主要的返回值。有什么想法吗?

/**
 * Recovers jpeg from raw file.
 *
 * */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>

#include "bmp.h"

int main(int argc, char *argv[])
{
    // ensure proper usage:1 file names as arg
    if (argc != 2)
    {
        fprintf(stderr, "Usage: ./recover infile\n");
        return 1;
    }

    char *infile = argv[1]; //name infile

    // create file pointer to read input file
    FILE *inptr = fopen(infile, "r");
    if (inptr == NULL)  //check that it is pointing somewhere
    {
        fprintf(stderr, "Could not open %s.\n", infile);
        return 2;
    }

    fseek(inptr, 0L, SEEK_END); //go to end of file
    int size = ftell(inptr);        //store length of file in bytes
    rewind(inptr);              //rewind to start of file. equivalent to: fseek(inptr, 0L, SEEK_SET);
    int num_blocks = size/512;      //total number of blocks. Note that the final E0F block (which might be less than 512 bytes)
                                    //will be lost but this is good

    uint8_t* buffer = malloc( 512 * sizeof(uint8_t)); //get buffer pointer to series of byte long ints
    if (buffer == NULL)  //check that it is pointing somewhere
    {
        fprintf(stderr, "Could not allocate memory.\n");
        return -1;
    }

    int jpg_count = 0;


    for (int block = 0; block < num_blocks; block++)
    {

        fread(buffer, 1, 512, inptr); // store a block in the buffer


        if (buffer[0] == 0xff &&
            buffer[1] == 0xd8 &&
            buffer[2] == 0xff &&
            (buffer[3] & 0xf0) == 0xe0)
        {
            char filename[10];   //create filename
            sprintf(filename, "%03i.jpg", jpg_count);

            jpg_count++;
            FILE *img = fopen(filename, "w"); //create file pointer to append to output file
            fwrite(buffer, 1, 512, img);   //copy Block to img
            fclose(img);

        }
        else
        {
            if (jpg_count > 0)
            {
                char filename[10];   //create filename
                sprintf(filename, "%03i.jpg", jpg_count -1);

                FILE *img = fopen(filename, "a");
                fwrite(buffer, 1, 512, img);
                fclose(img);
            }
        }
    }
    free(buffer);


    // close infile
    fclose(inptr);

    //if success
    return 0;
}

0 个答案:

没有答案