【发布时间】:2019-02-16 17:56:17
【问题描述】:
所以我是一个在 C 方面苦苦挣扎(真的溺水)的新手,试图通过 CS50 努力工作。我正在进行“恢复”练习,试图从 card.raw 文件中恢复 jpeg。通过谷歌搜索得知,通过在终端输入 xxd -l 2400 card.raw (char is 'L'),可以在终端显示字节 0-2384(含),格式如下:
0000000: 0000 0000 0000 0000 0000 0000 0000 0000 ....
0000950: 0fe0 c11b e555 8f20 33cc fbfe 559e 8eee .....U. 3...U...
Q1:我想使用 printf 显示前 32 个字节(全为 0)(这样我就可以验证正在读取的内容)。我的程序编译,但什么也没显示。 (当然,一旦我有这个工作,我会改变它以显示更多字节,因为我知道第一个 jpeg 从哪里开始查看终端中的数据)。
感谢简单的回答(如果我更有经验,我不会发布这样的基本问题)。谢谢,
#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);
}
【问题讨论】:
-
欢迎提出基本问题,但每个问题只能发布一个问题。
-
在十六进制转储中,左侧的数字是数据的偏移量,数据由中间的十六进制数据和右侧的这些十六进制值的 ascii 表示。 Hexdump 一个文本文件,你会看到它是如何工作的。
-
您也可以在此处查找有关 CS50 的问题和答案。 cs50.stackexchange.com