문제

How to get the absolute path for a given relative path programmatically in Linux?

Incase of Windows we have the _fullpath() API. In other words, I mean what is analogous API to _fullpath of Windows in Linux?

도움이 되었습니까?

해결책

As Paul mentioned, use realpath(). Please note though, that since many file systems in Linux support hard links, any given directory can have a number of different absolute paths.

다른 팁

Check out the realpath function.

#include <stdlib.h> 
#include <stdio.h> 
#include <linux/limits.h>
int main() 
{ 
        char resolved_path[PATH_MAX]; 
        realpath("../../", resolved_path); 
        printf("\n%s\n",resolved_path); 
        return 0; 
} 

Try realpath:

$ man realpath

This is also available in BSD, OS X, et al.

원하는 항목은 페이지에 동일한 캘린더에 대해 동일한 앱 파트를 추가하는 것입니다.나는 당신이하는 일이 새로운 캘린더 앱을 추가 한 다음 해당 새 캘린더를 페이지에 추가하는 것으로 생각합니다.피커가 두 번 동일한 캘린더 앱을 추가하고 두 번째 응용 프로그램 부분에서보기를 변경하면 찾고있는 것을 얻어야합니다.

Running on RedHat 5.3, realpath doesn't exist but readlink is installed. You can use it on relative paths and symlinks, plus it will resolve symlinks recursively for you. It's thus a better option that realpath in my opinion

readlink -f .

The is also another useful way, like "readlink -m $filename"

First of all, it works without requirement for target file to exist. Secondly, it will handle symlinks and get really real path.

// For C++ with Gnome Gtkmm3 libraries
#include <glibmm.h>
#include <giomm.h>

  string PathRel2Abs(string relpath) {
  Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(relpath);
  return file->get_path();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top