Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình C

Tìm thư mục làm việc hiện tại trong C/C++


Phần này xem cách lấy thư mục làm việc hiện tại dùng C/C++. Đã định nghĩa cờ cho hệ điều hành hiện tại.

Ví dụ mã

#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]; // bộ đệm chuỗi chứa đường dẫn
    GetCurrentDir(buff, FILENAME_MAX);
    string current_working_dir(buff);
    return current_working_dir;
}

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

Kết quả

D:\C++ Programs\section 53