加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-14 01:47:52 所属栏目:Linux 来源:网络整理
导读:#include stdio.h#include string.h#include errno.h#include fcntl.hint main(){ int fd; if ((fd = open("/home/zhangke",O_DIRECTORY | O_RDWR)) ==-1) { printf("error %sn",strerror(errno)); return -1; } return 0;} / home / zhangke是一个目录,它
#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 %sn",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).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读