在C++中,可以使用以下方法来找到当前的工作目录:
getcwd()
函数:#include <unistd.h>
#include <iostream>
int main() {
char path[FILENAME_MAX];
if (getcwd(path, sizeof(path)) != nullptr) {
std::cout << "Current working directory: " << path << std::endl;
} else {
std::cerr << "Failed to get current working directory." << std::endl;
}
return 0;
}
此方法通过getcwd()
函数获取当前工作目录的绝对路径,并将其存储在提供的缓冲区中。
#include <iostream>
#include <filesystem>
int main() {
std::filesystem::path path = std::filesystem::current_path();
std::cout << "Current working directory: " << path << std::endl;
return 0;
}
在C++17及以上版本中,可以使用<filesystem>标准库的current_path()
函数来获取当前工作目录的路径。
这些方法都可以简单地获取当前工作目录的路径,以便在程序中使用。同时,这些方法在不同的操作系统上都是可移植的。
请注意,本答案中不包含对腾讯云相关产品和产品介绍链接地址的推荐。
Game Tech
Elastic 实战工作坊
Elastic 实战工作坊
DB TALK 技术分享会
TVP技术夜未眠
云+社区技术沙龙[第21期]
腾讯技术创作特训营第二季
技术创作101训练营
云+社区技术沙龙[第16期]
领取专属 10元无门槛券
手把手带您无忧上云