इस खंड में, हम देखेंगे कि सी या सी ++ का उपयोग करके वर्तमान कार्यशील निर्देशिका कैसे प्राप्त करें। हमने मौजूदा ऑपरेटिंग सिस्टम के लिए कुछ फ़्लैग को परिभाषित किया है।
उदाहरण कोड
#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