Find out the current working directory in C/C++


在本部分中,我们将会学习如何使用 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]; //create string buffer to hold path
   GetCurrentDir( buff, FILENAME_MAX );
   string current_working_dir(buff);
   return current_working_dir;
}

main() {
   cout << get_current_dir() << endl;
}

输出

D:\C++ Programs\section 53

更新于:2019 年 7 月 30 日

6,000+ 次浏览

开启你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.