Linux C如何打开目录并获取文件描述符

Linux C如何打开目录并获取文件描述符,第1张

概述#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
#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 or O_RDWR is set).

总结

以上是内存溢出为你收集整理的Linux C如何打开目录并获取文件描述符全部内容,希望文章能够帮你解决Linux C如何打开目录并获取文件描述符所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1030003.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存