在 C/C++ 中查找当前工作目录
cc++server side programmingprogramming更新于 2025/3/9 17:52:17
在本节中,我们将了解如何使用 C 或 C++ 获取当前工作目录。我们为当前操作系统定义了一些标志。
示例代码
#ifdef WINDOWS #include <direct.h> #define GetCurrentDir _getcwd #else #include <unistd.h> #define GetCurrentDir getcwd #endif #include<iostream> using namespace std; std::string get_current_dir() { char buff[FILENAME_MAX]; //创建字符串缓冲区以保存路径 GetCurrentDir( buff, FILENAME_MAX ); string current_working_dir(buff); return current_working_dir; } main() { cout << get_current_dir() << endl; }
输出
D:\C++ Programs\section 53