通过stat with path检查dir是否存在

Checking if dir exist via stat with path

本文关键字:dir 是否 存在 检查 path stat with 通过      更新时间:2023-10-16

这可以编译,但我不相信它实际上是在检查正确的目录,看看它正在寻找的子目录是否存在。

    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,以便它打印一些东西,让我知道它是否失败,或者稍后在循环中使用,使您返回到开始处创建帐户或重新输入帐户名。

我认为这总结了一切。我可能解释错了一些,但据我所知,这就是我的理解。