C程序中如何获取当前文件夹的父目录?
我正在尝试获取包含该程序的当前文件夹的父目录。 我需要包含在我的 C 程序中。我尝试通过字符串方法来解决它,但我觉得可以有更好更简单的方法。例如:如果他的路径是“C:\Application\Config”,那么我想获取 - “C:\Application”只是父路径。
有人可以帮我解决这个问题吗?
谢谢, 普里扬卡
I am trying to get the parent directory of the current folder in which i have the program.
I need to include in the C program I have. I tried doing it through string methods and solve it, but I feel there can be a better and simpler way. Eg: If his path is “C:\Application\Config”, then I want to get - “C:\Application” the just parent path.
Can some one please help me with this?
Thanks,
Priyanka
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要就地截断字符串的最后一个反斜杠:
To in-place truncate a string at its last backslash:
有时,如果您不害怕
MAX_PATH
,只需添加\..
就足够了。Sometimes just adding
\..
will suffice if you are not afraid byMAX_PATH
.很难回答你的问题,因为你还没有真正指定你想用路径做什么。如果您想更改到新目录,这很简单,您只需使用通常用于更改目录的任何函数,但传递“..”而不是完整路径 - 这是因为在所有理智的文件系统上,“..”是一个“神奇”目录,存在于所有其他目录中并引用其父目录。
如果您想在跳转到新目录之前对其执行某些字符串函数,那么您的问题立即变得更加难以解决。我这样做的方式反映了 RichieHindle 的解决方案 - 将当前目录从完整路径中删除,然后你就剩下父目录的路径,你可以用它来随意处理。
It's difficult to answer your question since you haven't really specified what you want to -do- with the path once you have it. If you want to change to the new directory, that's easy, you just use whatever function you'd normally use to change directory but pass it ".." instead of a full path - that's because on all sane filesystems, ".." is a 'magic' directory which exists inside all other directories and refers to the parent thereof.
If you want to perform some string function on the new directory before jumping to it, your problem instantly becomes a lot more difficult to solve. The way I'd go about doing it mirrors RichieHindle's solution - strip the current directory away from the full path then you're left with the parent directory's path with which you can muck about to your heart's content.
在Windows操作系统中,您需要的API函数称为GetCurrentDirectory()。
http://msdn.microsoft.com/en -us/library/aa364934%28v=vs.85%29.aspx
In Windows OS, the API function you need is called GetCurrentDirectory().
http://msdn.microsoft.com/en-us/library/aa364934%28v=vs.85%29.aspx