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 運行
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301591.html
標籤:其他
上一篇:資料結構:如果寫代碼,就寫一棵樹
