C++ mac&windows 获取程序运行根目录

 mac 

#include "iostream"
#include <unistd.h>
using namespace std;
int main(int argc, char** argv)
{
    const int MAXPATH=250;
    char buffer[MAXPATH];
    getcwd(buffer, MAXPATH);
    printf("The current directory is: %s", buffer);
}

windows

#include <stdio.h>  
#include <direct.h>  
int main()  
{  
    const int MAXPATH = 250;
    char buffer[MAXPATH];
    getcwd(buffer, MAXPATH);
    printf("The current directory is: %s", buffer);
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取Windows当前用户的根目录,可以使用下面的C++代码: ```cpp #include <iostream> #include <string> #include <cstdlib> std::string getHomeDirectory() { char* homePath = getenv("USERPROFILE"); if (homePath == nullptr) { homePath = getenv("HOMEDRIVE"); std::string homePathStr(homePath); homePath = getenv("HOMEPATH"); homePathStr += homePath; return homePathStr; } return std::string(homePath); } int main() { std::string homeDirectory = getHomeDirectory(); std::cout << "Windows当前用户的根目录: " << homeDirectory << std::endl; return 0; } ``` 这段代码使用了C++的getenv函数来获取环境变量USERPROFILE或HOMEDRIVE和HOMEPATH的值,它们都表示当前用户的根目录。如果USERPROFILE不可用,则使用HOMEDRIVE和HOMEPATH拼接而成。最后将获取到的根目录打印出来。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++ 获取进程所在目录(全路径)](https://blog.csdn.net/lyshark_csdn/article/details/124938871)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [c++ 获取当前用户的根目录](https://blog.csdn.net/u010953266/article/details/50340361)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值