1

Trying to apply the structs from pset4/resize. Coming up with a quick prototype to make sure i understand structs completely.

1) I keep returning nothing in my output. Can someone tell me what's wrong with my code? I want to print at least 1 jpg into my output with my code. (I suspect its something to do with my data type?..)

2) Also, why does sizeof(card.raw)||sizeof(inptr)= 8 when the card.raw is around 17mb...?

#include <stdio.h>
#include "jpg.h"



int main(void)
{
    //ZYMAYLA'S ACTUAL PSEUDOCODE
    //open card file

    FILE* inptr = fopen("card.raw", "r");
    if(inptr == NULL)
    {
        printf("could not open file\n");
        fclose(inptr);
        return 1;
    }

    FILE* outptr = fopen("testOutput", "w");
    if(outptr == NULL)
    {
        printf("could not create file\n");
        fclose(outptr);
        return 2;
    }

    //repeat untill end of card
    IDENTIFIER id;
    unsigned char buffer;

        //read 512bytes into a buffer
        fread(&id, sizeof(IDENTIFIER), 1, inptr);

        //start of a new jpg?
        for(int i = 0; i < 17000000; i++)       //unable to use sizeof(card.raw).. why?
        {
            fseek(inptr, 1, SEEK_CUR);         //fseek(out, 0, SEEK_SET);

            if(id.identifierI == 0xff && id.identifierII == 0xd8 && id.identifierIII== 0xff 
            && (id.identifierIV == 0xe0 || id.identifierIV == 0xe1 || id.identifierIV == 0xe2 || 
            id.identifierIV == 0xe3 || id.identifierIV == 0xe4|| id.identifierIV == 0xe5|| id.identifierIV == 0xe6||
            id.identifierIV == 0xe7|| id.identifierIV == 0xe8|| id.identifierIV == 0xe8|| id.identifierIV == 0xe9||
            id.identifierIV == 0xea|| id.identifierIV == 0xeb|| id.identifierIV == 0xec|| id.identifierIV ==  0xed|| 
            id.identifierIV == 0xee|| id.identifierIV == 0xef))
            {
                printf("found jpeg!\n");
                fwrite(&id, sizeof(IDENTIFIER), 1, outptr);

                fread(&buffer, 512, sizeof(unsigned char), inptr);
                fwrite(&buffer, 512, sizeof(unsigned char), outptr);
            }
        }

            //yes... do..
            //no... do..
        //already found a jpg?
            //yes... do..
            //no... do..
    //close any remainding files
    fclose(inptr);
    fclose(outptr);
}

Here is my jpg.h file:

#include <stdint.h>
typedef uint8_t BYTE;

typedef struct
{
  BYTE identifierI;
  BYTE identifierII;
  BYTE identifierIII;
  BYTE identifierIV;
} __attribute__((__packed__))
IDENTIFIER;
1

You're overthinking this. A struct is not necessary here. Why not try storing your identifiers in an array instead?

5
  • 1
    hmmm I understand and have seen a few guys do it using the array method. I just thought it'd be a good time to practice using structs.. ? Can it technically be done? Dec 4 '16 at 15:23
  • Don't waste effort creating a struct to perform a conditional test on a few bytes of data coming out of another struct. Use a struct when you are creating a data structure made up of nodes (linked list, hash table, trie) or when defining a composite data type like the BMP file header. Dec 5 '16 at 1:40
  • 1
    Hmm.. alright.. I tried doing it with arrays and it's really tough too... would you help me out please? I've started another thread cause it's a completely diff topic. (cs50.stackexchange.com/questions/22185/…) This is not an easy course.. haha.. Dec 5 '16 at 19:29
  • It is a challenging course -- don't give up! I'll take a look at your other question. Dec 6 '16 at 1:14
  • Thanks! I'll try my best. It's also extremely tempting/easy to just look at a solution when it gets late at night and can't crack it.. Malan was right... (I asked too many dumb questions and was banned from stack overflow(main).. haha yike.. ) Dec 6 '16 at 16:15

You must log in to answer this question.

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