问题描述
我使用的是 CHDIR()
C函数允许用户更改目录。
I am using the chdir()
C function to allow a user to change directory.
的功能但是,不能识别'〜'。我需要做任何明确的转换,那么 CHDIR
不承认什么〜手段?因为我的不工作。还是我做错了什么?
The function however, doesn't recognize '~'. Do I need to do any explicit conversion, so chdir
doesn't recognize what ~ means? Because mine isn't working. Or am I doing something wrong?
推荐答案
波浪线扩展是由shell处理,而不是由一个系统调用。你可以使用的getenv()
来读取环境变量首页
,然后使用它作为参数传递给 CHDIR()
。
Tilde expansion is handled by the shell, not by a system call. You could use getenv()
to read the environment variable HOME
and then use that as the argument to chdir()
.
有系统调用来获取此信息,可能是一个单独的系统上更可靠,但它们不是完全可移植。你看,例如,在
There are system calls to get this information that may be more reliable on an individual system, but they're not completely portable. Look, for example, at getpwuid().
这篇关于CHDIR()到主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!