通过stat with path检查dir是否存在
Checking if dir exist via stat with path
这可以编译,但我不相信它实际上是在检查正确的目录,看看它正在寻找的子目录是否存在。
cout<< "Welcome back, '" << account << "'!n";
if (bool account = S_ISDIR(sb.st_mode)){
cout<<"Please enter your password: ";
}
else {
cout<< "There is no account by that name, please type NEW to create a new account.n";
所以我很好奇如何在这个例子中设置文件路径,以便它在父目录中进行检查。它在树中如下所示:
"home/user/Program/accounts/" + account
我不得不重做这个,Jonathan在指导我时对stat()的解释帮助很大,但下面是我如何让它工作的。
cout<< "Welcome back, '" << account << "'!n";
打印前面代码中提供的欢迎返回帐户名
struct stat sb;
创建stat的结构及其变量名。
if (stat(("/home/user/Account Program/accounts/"+account).c_str(),&sb) == 0 && S_ISDIR(sb.st_mode)){
cout<<"Please enter your password: ";
}
If检查。stat(打开上面结构化的stat调用,以路径开始("home…"),然后我通过+account传递我正在检查的dir,并通过+account创建的空字符串转换为正确的字符串).c_str()。然后检查它是否成功,如== 0,以及它是否是S_ISDIR。
else {
cout<< "There is no account by that name, please type NEW to create a new account.n";
}
这里我提供了一个Else,以便它打印一些东西,让我知道它是否失败,或者稍后在循环中使用,使您返回到开始处创建帐户或重新输入帐户名。
我认为这总结了一切。我可能解释错了一些,但据我所知,这就是我的理解。
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 在UNIX系统中使用DIR查找文件的字节大小
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 通过stat with path检查dir是否存在