0

As the title says, I've been trying to write a program in C that turns a file into multiple JPGs for a CS exercise. I've closed and freed everything I've worked with, but I'm still getting a segmentation fault, and I'm not quite sure why. Any advice wold be of great help.

EDIT I already found the answer, it's the fact that there's a possibility that the first block of bytes has no header, which makes the program go to the "else" conditional and write 512 bytes of nothing into a file that doens't exist. The solution was to turn else into else if (block[0] != 0xff && block[1] != 0xd8 && block[2] != 0xff && (block[3] & 0xf0) != 0xe0 && filenumber != 0) Hopefully this helps somebody!

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{
    // Checks for correct argv usage
    if (argc != 2)
    {
        printf("Usage: filter [file]\n");
        return 1;
    }

    FILE *f = fopen(argv[1], "r");
    if (f == NULL)
    {
    printf("Invalid file\n");
    return 1;
    }

    int filenumber = 0;
    BYTE *block = malloc(512 * sizeof(BYTE));

    char *filename = NULL;
    FILE *img = NULL;
    while (fread(&block, sizeof(BYTE), 512, f) == 512)
    {
        if (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && (block[3] & 0xf0) == 0xe0)
        {
             if (filenumber == 0)
             {
                filename = malloc(8);
                sprintf(filename, "%03i.jpg", filenumber);
                img = fopen(filename, "a");
                if (img == NULL)
                return 1;

                fwrite(&block, sizeof(BYTE), 512, img);
                filenumber++;
             }
             else
             {
                 fclose(img);
                 sprintf(filename, "%03i.jpg", filenumber);
                 img = fopen(filename, "a");

                 if (img == NULL)
                 return 1;

                 fwrite(&block, sizeof(BYTE), 512, img);
                 filenumber++;
             }
        }
        else
        {
            fwrite(&block, sizeof(BYTE), 512, img);
        }
    }

        fwrite(&block, sizeof(BYTE), fread(block, sizeof(BYTE), 512, f), img);
        free(filename);
        free(block);
        fclose(img);
        fclose(f);
        return 0;
}
  • 1
    `fread(&block, ...` and in `fwrite` should not have the `&`. You are passing the address of the pointer, instead of the buffer location. – Weather Vane Jan 19 '21 at 21:41
  • 1
    See my cs50 recover answer: https://stackoverflow.com/questions/63561945/cs50-recover-pset4-images-not-recovered/63565203#63565203 – Craig Estey Jan 19 '21 at 21:43
  • @EugeneSh. I'm not quite sure of the max size, but the problem never specified on that. What do you mean by "You don't close your files"? – Davidbowie123 Jan 19 '21 at 21:45
  • In fact I think I have misread the code, please disregard. – Eugene Sh. Jan 19 '21 at 21:48
  • It is best to avoid `malloc` when you don't need it. Just allocate those arrays on the stack. – Retired Ninja Jan 19 '21 at 22:01
  • the posted code does not compile! It is missing two critical statements: `#include ` and `#include ` – user3629249 Jan 20 '21 at 07:14
  • Does this answer your question? [CS50 recover segmentation fault](https://stackoverflow.com/questions/62152774/cs50-recover-segmentation-fault) – user3629249 Jan 20 '21 at 07:18

0 Answers0