有这样一段 C 代码,就是往文件里写入一个 int 型的 1:
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main() {
const char *path = "test.pid";
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
int v = 1;
if (fd > 0){
write(fd, &v, sizeof(int));
} else {
int error_code = errno;
const char* error_message = strerror(error_code);
printf("Failed to open file, reason: %s\n", error_message);
}
close(fd);
}
文件生成后,分别hexdump
和hexdump -C
查看,显示的顺序有点不一样:
➜ lane@vbox#2 ~ cat test.pid | hexdump -C
00000000 01 00 00 00 |....|
00000004
➜ lane@vbox#2 ~ cat test.pid | hexdump
0000000 0001 0000
0000004
一个是 0100 ,一个是 0001 。为啥呢? 看起来 hexdump -C 才是文件的实际存储顺序。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.