CS50-恢复-操作Card.raw PSET3 [英] CS50 - Recovery - Manipulating Card.raw PSET3

查看:207
本文介绍了CS50-恢复-操作Card.raw PSET3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是一名新手,正在C中苦苦挣扎(真的很困),试图通过CS50进行自己的尝试.我正在进行恢复"练习,试图从card.raw文件中恢复jpeg.通过Googling,我了解到,通过在终端中键入xxd -l 2400 card.raw(char为'L'),我可以在终端中显示0-2384字节(包括首尾),格式如下:

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 ................

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

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

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

Q1:我想使用printf显示前32个字节(全0)(以便我可以验证正在读取的内容).我的程序可以编译,但是什么也不显示. (当然,一旦完成此工作,我将其更改为显示更多字节,因为我知道第一个jpeg从查看终端中的数据开始的位置).

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);
}

推荐答案

感谢DinoCoderSAurus,在您的帮助下(以及其他一些帮助),我能够找到以下内容:

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;
}

这篇关于CS50-恢复-操作Card.raw PSET3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆