gpt4 book ai didi

检查文件是目录还是文件

转载 作者:太空狗 更新时间:2023-10-29 16:23:14 27 4
gpt4 key购买 nike

<分区>

我正在编写一个程序来检查某物是文件还是目录。有比这更好的方法吗?

#include <stdio.h>

#include <sys/types.h>
#include <dirent.h>
#include <errno.h>

int isFile(const char* name)
{
DIR* directory = opendir(name);

if(directory != NULL)
{
closedir(directory);
return 0;
}

if(errno == ENOTDIR)
{
return 1;
}

return -1;
}

int main(void)
{
const char* file = "./testFile";
const char* directory = "./";

printf("Is %s a file? %s.\n", file,
((isFile(file) == 1) ? "Yes" : "No"));

printf("Is %s a directory? %s.\n", directory,
((isFile(directory) == 0) ? "Yes" : "No"));

return 0;
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com