如何在C程序中获取当前文件夹的父目录? [英] How to get the parent directory of the current folder in a C program?

查看:441
本文介绍了如何在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?

谢谢,
Priyanka

Thanks, Priyanka

推荐答案

到在最后一个反斜杠处截断字符串:

To in-place truncate a string at its last backslash:

char pathname[MAX_PATH];
GetCurrentDirectory(MAX_PATH, pathname);
char* last_backslash = strrchr(pathname, '\\'); 
if (last_backslash)
{
    *last_backslash = '\0';
}

这篇关于如何在C程序中获取当前文件夹的父目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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