0

I am working on pset 4 but my program does not spit out any jpegs. I guess that it has something to do with the data type of the variable "buffer". How to specify an array for storing "bytes"?

#include <stdio.h>
#include <cs50.h>
#include <stdint.h>

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

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

    //open memory card
    FILE *inptr = fopen(infile, "r");

    if(inptr == NULL)
    {
        fprintf(stderr, "File could not be opened.\n");
        return 2;
    }

    //define tools
    char filename[20];
    int buffer[512];

    int n = 0;
    FILE *outptr = NULL;

    //read block size of 1 byte for 512 times until it reaches EOF (less than 512)
    while (fread(buffer, 1, 512, inptr) == 512)
    {

        //When find a JPEG beginning
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //close the last file (except for the 1st time) and open a new file
            if ( n != 0)
            {
                fclose(outptr);
            }

            sprintf(filename, "%03i.jpeg", n);
            outptr = fopen(filename, "w");
            n++;
        }

        //write bytes only if an output file is opened
        if (outptr != NULL)
        {
            fwrite(buffer, sizeof(buffer), 1, outptr);
        }
    }

    fclose(inptr);
    return 0;
}

1 Answer 1

0

Try researching c data types. Looks like you need a single byte unsigned integer type, like uint8_t maybe?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you very much! By declaring the the buffer array as " uint8_t " the program worked.
    – Elisa Gan
    Commented Jan 19, 2018 at 7:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .