问题
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);
}
回答1:
There are a couple of significant problems. First this declaration char text[32];
. Recall that char
has a very specific meaning, it is evaluated as integers from 0 to 255; it is "signed". That is perfect for reading ascii text. Recall/review bmp.h from resize
to see how data should be declared to read data that is not ascii text, like image data.
-- edit -- Binary data needs to be an "unsigned" data type. In bmp.h, the author used uint8_t
here typedef uint8_t BYTE;
(which requires #include stdint.h>
). You could useunsigned char text[32]
Secondly this printf("%s\n", text);
. text
is declared an array of chars. But remember the thing that makes a string a string? It is the terminating null byte, technically 0
. So when you ask printf to print text
as a string it will print everything up to the first null byte (0
). Which, as you can see from your hex dump, is the first byte in the file.
--edit-- Since you cannot use a string format in printf, you can print the ouptut one character at a time, much like mario or caesar. However, since it is unsigned, the format string would be %u
instead of %c
. You can see it in hex with the format string %04x
(x
is the specifier for hex).
回答2:
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;
}
来源:https://stackoverflow.com/questions/54726027/cs50-recovery-manipulating-card-raw-pset3