Cũng thử "getcwd"
#include <unistd.h>
char cwd[100000];
getcwd(cwd, sizeof(cwd));
std::cout << "Absolute path: "<< cwd << "/" << __FILE__ << std::endl;
Kết quả:
Absolute path: /media/setivolkylany/WorkDisk/Programming/Sources/MichailFlenov/main.cpp
Kiểm tra môi trường:
[email protected]$/ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.6 (jessie)
Release: 8.6
Codename: jessie
[email protected]$/ uname -a
Linux localhost 3.16.0-4-amd64 #1 SMP Debian 3.16.36-1+deb8u2 (2016-10-19) x86_64 GNU/Linux
[email protected]$/ g++ --version
g++ (Debian 4.9.2-10) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Nguồn
2016-12-18 19:40:56
Các 'một cộng' là không cần thiết, thuogh nó sẽ không làm hại gì cả. –
'GetFullPathName' trên Windows hoạt động cho các tệp không tồn tại. 'realpath' yêu cầu đường dẫn tồn tại. Đây là loại hút khi bạn muốn tạo một đường dẫn hoặc tập tin. – Joakim