1. pwd查看当前目录 在当前目录 mkdir zuoye02 新建目录
2. cd zuoye02 进入文件夹?
3.vim write.c
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
struct stu {
char name[20];
int id;
int age;
char sex;
};
int main() {
struct stu student[3] = {{"学生0", 1, 10, 'M'},
{"学生1", 2, 20, 'G'},
{"学生2", 3, 21, 'M'}};
int i,j = 0, fd = open("./stu.dat", O_RDWR | O_CREAT, 0644), buf = 0;
for (; i < 3; i++) {
write(fd, &student[i], sizeof(student[i]));
}
for (; j < 3; j++) {
lseek(fd, 24 + j * 32, SEEK_SET);
read(fd, &buf, sizeof(buf));
printf("学生%d的年龄为%d\n", j, buf);
}
close(fd);
return 0;
}
4. gcc -o write write.c
5. ./write 运行
|