0

After 12hrs of banging my head against this, and having trawled Stack Exchange, I've decided that it's time to ask for help!

Really not sure where I'm going wrong, but suspect it's related to the instruction to store each JPEG as a separate file in the current working directory. Struggling to get my head around the file manipulation functions, particularly how to combine these.

Think my code may be generating filenames but isn't linking these to output files. Not even sure that output files are being generated at all in fact! Doesn't seem like it, as I've managed to create one called recover that just seemed to be full of text.

Please find my code below. Any help with this gratefully received!

Many thanks!

SiƓn

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

typedef uint8_t BYTE;

int main(int argc, char *argv[])
{

    // Ensure exactly one CLA entered
    if (argc != 2)
    {
        fprintf(stderr, "Please enter exactly one valid filename.\n");
        return 1;
    }

    // Check filename is valid
    if (strcmp ("card.raw", argv[1]) != 0)
    {
        fprintf(stderr, "Invalid filename.\n");
        return 1;
    }

    // Open input file
    FILE *input_file = fopen(argv[1], "r");
    if (input_file == NULL)
    {
        fprintf(stderr, "Could not open %p.\n", input_file);
        return 1;
    }

    // Create pointer to array to store current block of input
    BYTE *buffer;

    // Allocate memory to read one 512 B block from input file
    buffer = (BYTE *) malloc(512 * (sizeof(BYTE)));

    // Create pointer to current output file
    FILE *output = NULL;

    // Allocate memory to store current output file
    output = (FILE *) malloc(sizeof(FILE));

    // Create pointer to string to store output filenames
    char *filename;

    // Allocate memory to string to store output filenames
    filename = (char *) malloc(7 * (sizeof(char)));

    // Initialise block_size variable to 512
    int block_size = 512;

    // Initialise output_count variable to 0
    int output_count = 0;

    // Initialise variable to control file processing and iterate through input file until end of file reached
    while (block_size == 512)
    {
        // Read current block into buffer
        block_size = fread(buffer, 1, 512, input_file);

        // If this is a new JPEG
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // If this isn't the first JPEG file from input...
            if(output_count > 0)
            {
                // Close last output file
                fclose(output);
            }

            // Name current output file using output_count
            sprintf(filename, "%03i.jpg", output_count);

            // Increment output_count
            output_count++;

            // Open output file for writing
            output = fopen("filename", "w");

            // Write current block to output file
            fwrite(buffer, 1, 512, output);
        }
        // Check if output process has begun
        else if(output_count > 0)
        {
            // Write current block to output file
            fwrite(buffer, 1, 512, output);
        }

    }

    // Close input file
    fclose(input_file);

    // Free memory allocated earlier in programme
    free(buffer);
    free(filename);
    free(output);
}
  • FYI I've had a breakthrough! I removed the quotation marks around the first argument of my fopen function here: output = fopen("filename", "w"); and now a bunch of JPEGs have been generated. Will comment again and/or mark this question answered once I've tested... – Sion Williams Jul 2 at 9:57
  • Quick confirmation that comment above resolved my main stumbling block. Having overcome one more hurdle covered here -cs50.stackexchange.com/questions/35461/… - I've checked and submitted my code successfully so will mark this as answered. Hope this may be useful for others struggling with similar challenges! – Sion Williams Jul 2 at 10:23
0

Removed the quotation marks around the first argument of my fopen function here: output = fopen("filename", "w");

| improve this answer | |

You must log in to answer this question.

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