C语言检测对象是文件还是文件夹的代码

本文分享了一段实用的C语言代码,用于检测指定路径的对象是普通文件还是目录。通过使用stat函数和S_ISREG、S_ISDIR宏,代码能够准确判断目标对象的类型,为开发过程提供便利。

把开发过程中比较常用的代码片段做个珍藏,如下的代码是关于C语言检测对象是文件还是文件夹的代码,应该能对各位朋友有用途。
bool file_exists
(
)
{
bool Exists;
struct stat Info;
if (stat(Pathname, &Info) == 0)
{
Exists = S_ISREG(Info.st_mode);
}
else
{
Exists = false;
return
Exists;

 





检测是否是文件夹


bool directory_exists
(
bool FollowSymlink
)
{
bool Exists;
struct stat Info;
if
(
(FollowSymlink ?
stat(Pathname, &Info)
:
lstat(Pathname, &Info)
)
==
0
)
{
Exists = S_ISDIR(Info.st_mode);
}
else
{
Exists = false;
return
Exists;




 

转载于:https://www.cnblogs.com/codeoldman/p/10372816.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值