C ++-确定Linux中是否存在目录(不是文件) [英] C++ - Determining if directory (not a file) exists in Linux

查看:798
本文介绍了C ++-确定Linux中是否存在目录(不是文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux中,如何使用C ++确定目录(不是文件)是否存在?我尝试使用stat()函数,但是在找到文件时返回正值.我只想查找输入的字符串是否是目录,而不是其他内容.

How would I determine if a directory (not a file) existed using C++ in Linux? I tried using the stat() function but it returned positive when a file was found. I only want to find if the inputted string is a directory, not something else.

推荐答案

根据 man(2)stat 您可以在st_mode字段上使用S_ISDIR宏:

According to man(2) stat you can use the S_ISDIR macro on the st_mode field:

bool isdir = S_ISDIR(st.st_mode);

请注意,如果您的软件可以在其他操作系统上运行,我建议您使用Boost和/或Qt4来简化跨平台支持.

Side note, I would recommend using Boost and/or Qt4 to make cross-platform support easier if your software can be viable on other OSs.

这篇关于C ++-确定Linux中是否存在目录(不是文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆