#include <stdio.h>#include <string.h>#include <errno.h>#include <fcntl.h>int main(){ int fd; if ((fd = open("/home/zhangke",O_DIRECTORY | O_RDWR)) ==-1) { printf("error %s\n",strerror(errno)); return -1; } return 0;}
/ home / zhangke是一个目录,它存在.我得到错误是一个目录,所以,我怎样才能使用open()来正确获取目录的fd?
解决方法 使用O_RDONLY而不是O_RDWR作为访问模式.从open(2)错误列表:总结
EISDIR
pathname refers to a directory and the access requested involved writing (that is,O_WRONLY
orO_RDWR
is set).
以上是内存溢出为你收集整理的Linux C如何打开目录并获取文件描述符全部内容,希望文章能够帮你解决Linux C如何打开目录并获取文件描述符所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)