Search code examples
ccs50recover

CS50 - Recovery - Manipulating Card.raw PSET3


So I'm a newbie struggling (drowning really) with C, trying to work my way through CS50. I'm working on the 'Recover' exercise, trying to recover jpegs from the card.raw file. Through Googling, I have learnt that by typing xxd -l 2400 card.raw (char is 'L') in terminal, I can display bytes 0-2384 inclusive in terminal, which are in the following format:

0000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................

0000950: 0fe0 c11b e555 8f20 33cc fbfe 559e 8eee .....U. 3...U...

Q1: I want to display the first 32 bytes (all 0's) using printf (so I can verify what is being read). My program compiles, yet displays nothing. (Of course, once I have this working, I'll change it to display more bytes, as I know where the first jpeg starts from looking at the data in terminal).

Simple responses are appreciated (if I was more experienced, I wouldn't be posting such basic questions). Thanks,

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

int main()
{

    // hardcode opening of card.raw in read binary mode
    FILE *infile = fopen("card.raw", "rb");

    if (infile == NULL)
    {
        fprintf(stderr, "Could not open infile"); 
        return 2;
    } 

    // declare a variable to hold data to be read from infile file, note that a size for it must be specified
    char text[32];

    /* go to the beginning of the card.raw file to start reading */
    fseek(infile, 0, SEEK_SET);

    // text is the variable that will hold what is read, declared above
    // how many to read, how many to read at a time, where to read from
    fread(text, 32, 1, infile);
    printf("%s\n", text);
}

Solution

  • Thanks DinoCoderSAurus, with your (and some other help), I was able to figure out the following:

    #include <stdio.h>
    #include <stdint.h>
    #include <stdlib.h>
    
    int main()
    {
    
        // hardcode opening of a file with fopen, in read binary mode
        FILE *infile = fopen("card.raw", "rb");
        // error check, did file open?
        if (infile == NULL)
        {
            fprintf(stderr, "Could not open infile"); 
            return 2;
        }
    
        // because card.raw contains binary/hex data, must use unsigned char to hold data, 32 bytes chosen at random
        unsigned char dataval[32];
    
        //    dataval is the variable that will hold what is read, declared above
        //          how many to read, how many to read at a time, where to read from
        fread(dataval, 1, 32, infile);
    
        //Print bytes (from dataval) one at a time
        for (int i = 0; i < 32; i++)
        {
            printf("%02X ", (int)dataval[i]);
        }
        printf("\n");
    
        return 0;
    }