为什么MKDIR失败,符号(〜)工作? [英] Why mkdir fails to work with tilde (~)?

查看:475
本文介绍了为什么MKDIR失败,符号(〜)工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写

mkdir("~/folder1" , 0777);

在Linux中,它未能创建一个目录。如果我替换与膨胀的主目录,它工作正常。什么是使用问题

in linux, it failed to create a directory. If I replace the ~ with the expanded home directory, it works fine. What is the problem with using ~ ?

感谢

推荐答案

只知道到外壳,而不是在的mkdir 系统调用。

~ is known only to the shell and not to the mkdir system call.

但是,如果你尝试:

system("mkdir ~/foo");

这个工程作为的mkdir〜/ foo的传递到外壳和外壳程序扩展 $ HOME

this works as the "mkdir ~/foo" is passed to a shell and shell expands ~ to $HOME

如果你想使用 $ HOME 的mkdir ,你可以利用的<一的HREF =HTTP://en.cp$p$pference.com/w/cpp/utility/program/getenv相对=nofollow> GETENV 功能:

If you want to make use of the $HOME with mkdir, you can make use of the getenv function as:

char path[MAX];
char *home = getenv ("HOME");
if (home != NULL) {
        snprintf(path, sizeof(path), "%s/new_dir", home);
        // now use path in mkdir
        mkdir(path, PERM);
}

这篇关于为什么MKDIR失败,符号(〜)工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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