【发布时间】:2014-04-10 12:04:22
【问题描述】:
我想读取一个包含一些十六进制值的文件。比如这个:
^@\352\203\363
或者当您将 emacs 置于 hexl-mode 时:
00ea 83f3
这是我试图打开的文件的链接: http://www.partage-facile.com/0L9FOY7ERB/toto.cor.html
现在这是我尝试阅读和打印的主要内容:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int ac, char **av)
{
int fd;
char tmp[4];
if ((fd = open(av[1], O_RDONLY)) == -1)
{
printf("Open failed.\n");
return (-1);
}
if ((read(fd, tmp, 4)) == -1)
{
printf("Read failed.\n");
return (-1);
}
printf("%s", tmp); /* Here I don't know really what to do to print. */
if ((close(fd)) == -1)
{
printf("Close failed.\n");
return (-1);
}
return (0);
}
但它什么也没给我打印,只是空白。如果我尝试使用 printf 打印(使用 %x 等标志表示十六进制,它会随机值)
【问题讨论】:
-
你需要告诉我们你得到了什么,出了什么问题。
-
它什么也没给我打印,只是空白。如果我尝试使用 printf 打印(使用 %x 等标志表示十六进制,它会随机值)
-
好的,现在把评论变成一个句子,然后编辑你的问题,让它这样说。
-
是不是因为你的代码编译失败?我需要
#include <fcntl.h>才能编译。但是当它发生时,它就起作用了。 -
是的,我太傻了,忘记了 open() 的一个包含。但仍然对我不起作用。好吧..如果我手动写“^@\352\203\363”,那工作,但我得到的那个是用红色写的。这是我正在打开的文件的链接。 partage-facile.com/0L9FOY7ERB/toto.cor.html