以下是一个使用getcwd函数来优化的示例代码:
#include <iostream>
#include <unistd.h>
int main() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
std::cout << "Current working directory: " << cwd << std::endl;
} else {
std::cerr << "Error getting current working directory" << std::endl;
return 1;
}
return 0;
}
这段代码使用getcwd函数来获取当前工作目录,并将其打印出来。使用getcwd函数可以简洁地获取当前工作目录,而不需要额外的路径处理操作。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>