Trong phần này, chúng ta sẽ xem cách lấy thư mục làm việc hiện tại bằng C hoặc C ++. Chúng tôi đã xác định một số cờ cho hệ điều hành hiện tại.
Mã mẫu
#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;
} Đầu ra
D:\C++ Programs\section 53