0

Can't seem to find what is happening with my code, please help! I've been stuck in this problem for way too long. The jpg's are not created when I run the code.

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


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

    /* verify just one file */
    if (argc != 2)
    {
        printf("You should input just one file\n");
        return 1;
    }

    /* verify file is readable */

    char file_name[8];
    FILE *file = fopen(argv[1], "r");
    FILE *img = NULL;

    if (file == NULL)
    {
        printf("File not readable\n");
        return 1;
    }

    // asign buffer size
    unsigned char buffer[512];

    // image counter for file naming
    int count = 0;

    // keeps reading the file meanwhile the block is size 512
    while (fread(buffer, 512, 1, file) == 1)
    {

        // checks the first for bytes to know if its the beginning of a jpg
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            // if it's the first image create and write
            if (img == NULL)
            {
                fclose(img);
                sprintf(file_name, "%03i.jpg", count);
                img = fopen(file_name, "w");
                fwrite(buffer, 512, 1, img);

                count += 1;
            }
            // if it's other but first jpg closes previous file and create a new one and write
            else
            {
                fclose(img);

                sprintf(file_name, "%03i.jpg", count);
                img = fopen(file_name, "w");
                fwrite(buffer, 512, 1, img);

                count += 1;
            }
        }
        // keeps writing until finding a new jpg
        else
        {
            fwrite(buffer, 512, 1, img);

        }
    }
}
1

Little bit better maybe

    while (fread(bytes, BLOCK_SIZE, 1, file))
    {
        if (bytes[0] == 0xff && bytes[1] == 0xd8 && bytes[2] == 0xff && (bytes[3] & 0xf0) == 0xe0)
        {
            if (!firstJPG)
            {
                fclose(img);
            }

            else
            {
                firstJPG = false;
            }

            //open new jpg file to write data
            sprintf(FileName, "%03i.jpg", jpegCounter);
            img = fopen(FileName, "w");
            jpegCounter++;

        }

        //write data to jpg file
        if (!firstJPG)
        {
            fwrite(&bytes, 512, 1, img);
        }
    }
| improve this answer | |
1

Found myself the answer, what was happening was that the first bytes weren't the beginning of a jpg so it went straight to write:

  else
    {
        fwrite(buffer, 512, 1, img);

    }

Since there wasn't any file opened (img == NULL) it tried to write there and showed the error.

I just added a new if in that part and solved the problem:

else
{
    if (img != NULL)
    {
        fwrite(buffer, 512, 1, img);
    }

}
| improve this answer | |

You must log in to answer this question.

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