- 论坛徽章:
- 1
|
各位能帮我解答一下这是为什么吗?- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <stdio.h>
- struct student {
- int age;
- char name[20];
- };
- int main()
- {
- int fd; char buf[] = "hello world!\n";
- struct student chen = {22, "Jeremy Chen"};
- struct student chen_copy;
- fd = open("test.txt", O_CREAT | O_TRUNC | O_WRONLY | O_SYNC, S_IWUSR | S_IRUSR);
- write(fd, &chen, sizeof(chen));
- printf("cur offset %d\n", lseek(fd, 0, SEEK_CUR));
- lseek(fd, 0, SEEK_SET);
- printf("cur offset %d\n", lseek(fd, 0, SEEK_CUR));
- read(fd, &chen_copy, sizeof(chen_copy));
- printf("chen_copy age:%d, name : %s\n", chen_copy.age, chen_copy.name);
- // lseek(fd, 0, SEEK_SET);
- // printf("cur offset %d\n", lseek(fd, 0, SEEK_CUR));
- //
- // read(fd, &chen_copy, sizeof(chen_copy));
- // printf("cur offset %d\n", lseek(fd, 0, SEEK_CUR));
- // printf("chen_copy age:%d, name : %s\n", chen_copy.age, chen_copy.name);
- close(fd);
- }
复制代码 打开一个文件后,写内容,然后在保持打开的情况下将文件偏移置0,然后读刚写的内容。读不出来呢?
想了几天也没想出来,有人能帮我解释一下吗? |
|