How can I check whether this is directory-path or any filename-path?

后端 未结 3 840
暗喜
暗喜 2021-01-06 12:22

by this

Why does fopen("any_path_name",'r') not give NULL as return?

i get to know that in linux directories and files are considered to b

3条回答
  •  不思量自难忘°
    2021-01-06 12:46

    thanks zed_0xff and lgor Oks

    this things can be check by this sample code

    #include
    #include 
    #include 
    #include 
    int main()
    {
    struct stat statbuf;
    
    FILE *fb = fopen("/home/jeegar/","r");
    if(fb==NULL)
        printf("its null\n");
    else
        printf("not null\n");
    
    stat("/home/jeegar/", &statbuf);
    
    if(S_ISDIR(statbuf.st_mode))
        printf("directory\n");
    else
        printf("file\n");
    return 0;
    }
    

    output is

    its null
    directory
    

提交回复
热议问题