简体   繁体   English

c ++检查Unix中是否存在目录,如果存在,则调用void函数。

[英]c++ Checking if a directory exists in Unix and call void function if it exists.

So I'm trying to implement linux command rm -rf and in my main i have something like: 因此,我试图实现linux命令rm -rf,而在我的主机中我有类似以下内容:

struct stat sb;
if(unlink(argv[i]) == 0)
{
    printf (argv[i]); printf(" Deleted\n");
}
if (S_ISDIR(sb.st_mode))
{   
    remove_dir(argv[i]);
}
else 
{
    perror(argv[i]);
}

What I am trying to do is that if the user input is directory, then call the void remove_dir(); 我想做的是,如果用户输入是目录,则调用void remove_dir();。 to delete the directories, but instead it just prints whether argv[i] is directories or not. 删除目录,而是只显示argv [i]是否为目录。 Any suggestions would be very helpful. 任何建议将非常有帮助。

You need to populate struct stat sb using the function stat : 您需要使用stat函数填充struct stat sb

struct stat sb;
if (stat(argv[i], &sb) != 0) 
{
    perror(argv[i]);
}

Then, and only then, can you use S_ISDIR(sb.st_mode) . 然后,直到那时,您才能使用S_ISDIR(sb.st_mode)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM