Welcome fellow students, register yourself and dive into asking questions, giving answers and helping the community learn with you.

Pset3 Recover -- I don't know what I did wrong

0 votes
asked Jul 26 in Pset3 by emmawidl (140 points)
Hey!!

I am really struggling to understand what I did wrong in my project... I've stared at it for hours and just need some fresh eyes to look over it! Any help is appreciated x

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main(int argc, char *argv[])
{
    // Ensure you have one argument
    if (argy != 2)
    {
        fprint(stderr, "What file do you want to open?")
        return 1;
    }

    // Open the file (entered on command line)
    FILE *file_input = fopen(argv[1], "r");

    // Check if the file exists
    if (file == NULL)
    {
        fprint(stderr, "File %s does not exist\n", argv[1]);
        return 2;
    }

    // Create outfile
    FILE *img = NULL;

    // create file names, buffer & flag
    unsigned char buffer[512];
    char jpg_name[8];

    // set jpeg counter to 0 & flag to false
    int jpg_counter = 0;
    bool flag = false;

    // READ file (card.raw)
    while (fread(buffer, 512, 1, file_input) == 1)
    {
        //Beginning of a new JPEG?
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //If JPEG already exists, close it
            if (flag == true)
            {
                fclose(img);
            }
            else
            {
                flag = true;
            }

            sprintf(jpg_name, "%03i.jpg", jpg_counter);
            img = fopen(jpg_name, "w");
            jpg_counter++;
        }

        if (flag == true)
        {
            // write info into current jpeg file
            fwrite(&buffer, 512, 1, img);
        }

    }
    // close all files
    fclose(file_input);
    fclose(img);

    return 0;
}

Please log in or register to answer this question.

Welcome to CS50xHelpers Q&A, where you can ask questions and receive answers from other members of the community.

195 questions

248 answers

217 comments

4,622 users

2 Online
0 Member And 2 Guest
...