We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
我不完全确定您将如何处理当前工作目录的结果,因为该目录仅在保持打开状态时才会继续存在 - 您无法在该目录中创建新文件,并且该目录必须为空,以便可以将其删除 - 但您可以在
/ 上使用
来发现名称:readlink(2)
proc/self/cwd这是我的getcwd.c:
这是我的proccwd.c:
mu 太短 是正确的,他对
chdir("/");
的建议是正确的,如果它是一个守护进程——我可以想象你可能有充分的理由让你的程序否则知道其当前工作目录,并且甚至知道如果路径名仍然存在的话它可能是什么——但一般来说,你不应该关心。路径名"."
几乎在所有需要当前工作目录的情况下都有效,直到您需要为该路径实现内置的pwd
shell用户。I'm not entirely sure what you will do with the results of the current working directory when the directory will continue to exist only as long as it is held open -- you can't create new files in the directory, and it had to be empty so it could be deleted -- but you can use
readlink(2)
on/proc/self/cwd
to discover the name:Here's my
getcwd.c
:And here's my
proccwd.c
:mu is too short is correct with his advice to
chdir("/");
if it is a daemon -- I can imagine that you might have a good reason for your program to otherwise know its current working directory, and even have an idea of what the pathname might have been if it did still exist -- but in general, you shouldn't care. The pathname"."
will work in just about every case where it makes sense to need the current working directory, until you need to implement apwd
shell built-in for the user.试试这个。
Try this.
获取 $PWD 环境变量并将其缓存在您的应用程序中。
get the $PWD environment variable and cache it in your application.