2014-12-02 14 views
6

C/C++을 사용하여 해당 파일의 전체 경로를 반환하는 함수를 구현하는 방법을 배우지 못하게 도와주십시오.파일 이름을 지정하면 전체 경로를 얻는 방법은 무엇입니까?

+3

만약에 다른 위치에서 같은 이름을 가진 두 개의 파일? – Himanshu

+0

파일 이름이나 파일 포인터가 있습니까? – nikhilr57

+2

다음과 동일합니다 : http://stackoverflow.com/questions/1661982/how-do-i-get-the-full-path-for-a-filename-command-line-argument – Neska

답변

11

유닉스/리눅스 :

#include <limits.h> 
#include <stdlib.h> 

char *full_path = realpath("foo.dat", NULL); 

... 

free(full_path); 

나 :

char full_path[PATH_MAX]; 
realpath("foo.dat", full_path); 

윈도우 :

#include <windows.h> 

TCHAR full_path[MAX_PATH]; 

GetFullPathName(_T("foo.dat"), MAX_PATH, full_path, NULL); 
+0

보편적 인 해결책은 없습니까? – Pedro77

+1

C++ 17에는 ['std :: filesystem :: canonical'] (http://en.cppreference.com/w/cpp/filesystem/canonical)이 있습니다. 구형 표준의 경우 [boost :: filesystem'] (http://www.boost.org/doc/libs/1_64_0/libs/filesystem/doc/index.htm) 라이브러리의 일부로 사용할 수 있습니다. 비록 C 표준 라이브러리에 비슷한 기능이 있다고 생각하지 않습니다. – Wintermute

관련 문제