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
", text);
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…