如何检查字符串是否指定目录
How check if string specifies directory?
假设有一个字符串:
std::string some_string = "some_string";
我想知道chdir(some_string.c_str())
是否会在不调用它的情况下返回-1。有快速的方法吗?
附言:我希望我的代码也适用于windows,在那里我将使用_chdir()
#ifdef WIN32
# include <io.h>
#else
# include <unistd.h>
#endif
int access(const char *pathname, int mode);
// check user's permissions for a file
int模式值:
00-仅存在,02-只写,04-只读,06-读写。
如果文件具有给定的模式,则函数返回0。
我会使用Boost的is_directory函数,您可以在Boost的Filesystem Reference页面上找到更多信息。
我想知道
chdir(some_string.c_str())
是否会在不调用的情况下返回-1
你在做这些检查时需要小心。问题是,如果你依赖于它们的结果,因为在你进行检查和执行依赖于检查的操作之间,另一个进程可能执行了一个操作(在这种情况下是rmdir
(,该操作会使代码中的假设无效。也就是说,您可以在代码中引入种族风险。
由于希望它适用于Windows,请使用返回File Attribute Constants
的GetFileAttributes()
。
CCD_ 7甚至更好。
相关文章:
- 是否可以在c++中处理字符串流中的各个元素
- 是否可以从格式字符串中检索"width"
- C++-字符串是否包含一个带有简单循环的单词
- 将常量字符串添加到非常量字符串是否会给出常量字符串
- 是否可以将带有字符串化运算符的宏转换为 constexpr?
- 检查字符串是否"null" C++
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 是否有通用方法可以找到任何以 null 结尾的字符串的长度?
- 检查字符串是否为整数
- 是否有可能实现O(N)时间和O(1)空间解决方案,以实现C++中的字符串循环移位
- 我遇到了这个代码片段,不明白. 它递归检查 C++ 字符串中是否存在大写字符
- 返回从字符串文本创建的静态string_view是否安全?
- 如何检查模板类中的变量是否为字符串类型?
- 是否保证相同内容字符串文本的存储相同?
- 检查数组中是否有字符串中的值,如果没有,则添加它
- 在这种情况下,使用 string_view 是否会导致不必要的字符串复制?
- 使用 SET(C++) 检查两个给定字符串是否是字谜时出现运行时错误
- 是否可以创建一个用户定义的文本,将字符串文本转换为 own 类型的数组?
- 如何检查字符串中是否有重复的负号?例如 --1
- 如何检查字符串是否正确以将其转换为 int?