0

I have a problem with accessing first 4 integers of the 512 B pointer. Should I go with the struct? What would be approach to reference first 4 integers? Here is the line I am concerned of:

// check if a new jpg starts
if  (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && \
    (block[3] >= 0xe0 && block[3] <= 0xef))

A whole program:

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

int main(void)
{
    char* card = "card.raw";
    char title[8];
    bool found = false;
    int counter = 0;
    FILE* img;
    int *block;

    // allocating memory for a block
    block = (int *) malloc(512);

    // checking if memory has been allocated
    if (block == NULL)
    {
      fprintf(stderr, "malloc failed\n");
      exit(EXIT_FAILURE);
    }

    // open an input file and check if opens
    FILE* cardptr = fopen(card, "r");

    if (cardptr == NULL)
    {
        printf("Could not open %s.\n", card);
        return 2;
    }

    // looping through the file and transferring data
    while (fread(&block, sizeof(block), 1, cardptr) == 1)
    {
        // check if a new jpg starts
        if  (block[0] == 0xff && block[1] == 0xd8 && block[2] == 0xff && \
            (block[3] >= 0xe0 && block[3] <= 0xef))
        {
            // close if another is already opened
            if (found)
              fclose(img);

            // creating a new jpg file
            sprintf(title, "%03d.jpg", counter);
            img = fopen(title, "w");
            counter++;

            // checking if able to create
            if (img == NULL)
            {
                fclose(img);
                fprintf(stderr, "Could not create %s.\n", title);
                return 3;
            }

            found = true;
        }

        // check if jpg already exists
        if (found)
        {
          fwrite(block, sizeof(block), 1, img);
        }
    }

    fclose(cardptr);
    free(block);

    return 0;
}
0

Solved the problem by creating a structure and allocating memory within that structure.

Preprocessor:

typedef struct
{
    unsigned int f1:8;
    unsigned int f2:8;
    unsigned int f3:8;
    unsigned int f4:8;
    unsigned int *frest;
} BLOCK;

Main:

    BLOCK bl;

    // allocating memory for a block
    bl.frest = (unsigned int *) malloc(508);

You must log in to answer this question.

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