0

PSET 4 is really taking me for a wild ride that I am not enjoying whatsoever.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
int num = 0;
typedef uint8_t BYTE;
char *jpgName = "       ";
int main(int argc, char *argv[])
{

    // Set command line argument I want to accept
    char *verify = "card.raw";

    // Compare acceptable command line argument with inputted argument. Only provide error if incorrect.
    if(strcmp(argv[1], verify) == 0 && argc == 2)
    {
       //If argument is accepted give yourself a pat on the back
       printf("Good Job.\n");

    }
    else
    {

        printf("Try again Bozo\n"); //try again
        return 1;
    }

    char *infile = argv[1]; //Change the name of argv[1] to *infile for some reason?



    BYTE buffer[512]; //Create my 512 byte array

    FILE *f=fopen(infile, "r");//open card.raw file

    fread(&buffer, sizeof(BYTE), 512, f);//read my card.raw file into buffer
    
    //Make sure that the file is a JPEG.
    if(buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
    {

        //Create the name of my jpgfile.
        sprintf(jpgName, "%03i.jpg", num);
        //Increment it 
        num++;
        // Open a new file called *img with the jpgName
        FILE *img = fopen(jpgName, "w");
        // Write to the jpgName file
        fwrite(&buffer ,sizeof(BYTE), number ,*img)

    }

}

I don't really have any way to check if what I am doing is in any way correct before I actually finish the PSET and I feel stuck at this point. I have a suspicion that fread(&buffer, sizeof(BYTE), 512, f); should actually be fread(&buffer, sizeof(buffer), 1, f); but I am not sure. Is the size of the element the entire array and there is only one of them? Or is the size of each element one byte and there are 512 of them?

How does the fwrite program know when to terminate if the image is larger than one single 512 byte array? I've watched the video multiple times now and don't know how to continue.

This PSET seems a lot trickier than the previous ones.

1 Answer 1

1

RE:fread - it doesn't matter either way. The fread call is going to multiply the sieof() value x the number of them ( or parameter 2 x parameter 3 ) and read that many bytes. You just need to make sure that the sizeof(buffer) is 512 in this case. Either way, it will be 512 bytes read.

The program doesn't know how big the file is. Go back to the spec. The fine print is this. Once the garbage data at the front is discarded, everything that follows is part of an image. So, once you find a signature block, everything that follows is part of the same image until you find the next signature block. Then, it's time to start a new output file.

Maybe the code needs to have a loop added somewhere? ;-)

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

3
  • Now I am stuck trying to figure out how to allocate memory for the sprintf memory function.
    – Multiplify
    Apr 18, 2021 at 22:14
  • char filename[8];
    – Cliff B
    Apr 18, 2021 at 23:32
  • I just malloc(sizeof(char)*8), essentially the same thing right?
    – Multiplify
    Apr 19, 2021 at 13:50

You must log in to answer this question.

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