#include <stdio.h> #include <unistd.h> #include <fcntl.h> /*a.txt的内容 用vim "%!xxd编辑过"**/ /* 0000000: 4201 0000 6865 6c6c 6f77 6f72 6c64 0000 B...helloworld.. 0000010: 0000 0000 0000 0000 0a ......... */ /* *分析:数值在文件中存储是以十六进制存储的,文本是以ASCII码存储的 4201 是322的存储方式, 01是高位,42是低位 6865 6c6c 6f77 6f72 6c64 : helloworld的存储方式 68:'h' 65:'e' 6c:'l' 6c:'l' 6f='o' 77='w' 6f='o' 72='r' 6c='l' 64='d' */ struct demo { unsigned long num; char str[20]; } de = { 322, "helloworld" }; int main(int argc, char **argv) { int fd = open("a.txt", O_CREAT | O_RDWR); unsigned long num = 322; if (fd == -1) { return -1; } // write num and open with vi to see hex format ":%!xxd" . printf("szieof de is %d/n", sizeof(de)); write(fd, &de, sizeof(de)); return 0; }