Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

c - 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
", text);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 use
unsigned char text[32]

Secondly this printf("%s ", 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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...