创建一个结构体,里面有整数型和字符型的变量,在主函数里定义两个结构体变量,一个用来保存写入文件的数据,一个用来保存读出来的数据:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
struct Text{
int a;
char c;
};
int main()
{
int fd;
struct Text data[2] ={{10,'a'},{11,'b'}};
struct Text data2[2];
fd = open("./flie1", O_RDWR|O_CREAT, 0600);
write(fd, &data, sizeof(struct Text) * 2);
lseek(fd, 0, SEEK_SET);
read(fd, &data2, sizeof(struct Text) * 2);
printf("%d, %c\n", data2[0].a, data2[1].c);
printf("%d, %c\n", data2[1].a, data2[1].c);
close(fd);
return 0;
}

被折叠的 条评论
为什么被折叠?



