|
|
按照apue上的一个例子,我输入:
- #include <fcntl.h>
- #include "apue.h"
- char buf1[] = "abcdefghij";
- char buf2[] = "ABCDEFGHIJ";
- int
- main()
- {
- int fd;
-
- if((fd = open("file.hole", O_CREAT | O_WRONLY | O_TRUNC, FILE_MODE) < 0))
- err_sys("create error");
-
- if(write(fd, buf1, 10) != 10)
- err_sys("write buf1 error");
-
- if(lseek(fd, 16384, SEEK_SET) == -1)
- err_sys("lseek error");
-
- if(write(fd, buf2, 10) != 10)
- err_sys("write buf2 error");
-
- exit(0);
- }
复制代码
原来的代码是用create打开的,可是我的系统上好像没有create这个函数?于是我换成了open。可是虽然执行之后file.hole文件是创建了,但是事实上是往标准输出写的,而且提示lseek出错:
- ./a.out
- abcdefghijlseek error: Illegal seek
复制代码
我跟踪了一下,发现打开的那个fd事实上是0, 就是默认的标准输出吧?是不是shell自动为我打开了标准输出,可是这样的话,open的时候就不会再打开0了呀?还是其他什么原因呢? |
|