0

Okay so I am starting the recover program from Pset4 and I initially just want to get some output, any output, I have been trying to read the first few bytes from recover.raw but for some reason seem to get a NULL value no matter what I do. Here is what I am starting off with before I implement the rest of the program which should be pretty straightforward.

#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <math.h>
#include <string.h>
#include "bmp.h"

int main(int argc, char *argv[]){
    if(argc != 2){
        fprintf(stderr, "Usage: ./recover image");
        return 1;
    }

    FILE *infile = fopen(argv[1], "r");
    if(infile == NULL){
        fprintf(stderr, "File was not opened for reading.");
        return 2;
    }
    fseek(infile, 0, SEEK_SET);
    int *buffer[128];
    fread(buffer, sizeof(buffer), 1, infile);
    if(buffer[0] > 0){
       printf("YES\n");

    }
}

why is this not printing out anything?

1

You read the first 512 bytes (I used an array of uint8_t instead of int, but that should be possible as well), and print the first integer, representing the first 4 bytes.

If you throw card.raw int the hex viewer, you'll see the first 512 bytes really are all-zeros:

~/workspace/freestyle/ $ xxd -l 512 -g 8 -c 32 card.raw                                                                                                                                                                            
0000000: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000020: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000040: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000060: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000080: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
00000a0: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
00000c0: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
00000e0: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000100: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000120: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000140: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000160: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
0000180: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
00001a0: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
00001c0: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
00001e0: 0000000000000000 0000000000000000 0000000000000000 0000000000000000  ................................
~/workspace/freestyle/ $ 

I would encourage you to look at a few more of those lines to understand what you can expect to read. The first non-zero byte is in the second sector (byte 512), the first JPEG image starts in the third (byte 1024).

So there's nothing wrong with your programme. Sure, you could read more sectors, and you could explicitly fclose your input file at the end. But reading zero is not a problem with your code.

1
  • Yes after further testing with hexdump I realized that I was assuming that the first JPEG image would start at the first byte of the card.raw, silly assumption thanks for your help aswell! – Anthony Then Jun 29 '18 at 16:09

You must log in to answer this question.

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