2009-09-08 7 views
2

쿠분투 리눅스에서 C++을 사용하여 sys/stat.h의 stat를 사용하여 파일 소유자의 액세스 권한을 얻으려면 어떻게해야합니까?C++과 stat을 사용하여 소유자의 액세스 권한을 얻으십시오

struct stat results; 

    stat(filename, &results); 

    cout << "File type: "; 
    if (S_ISDIR(results.st_mode)) 
    cout << "Directory"; 
    else if (S_ISREG(results.st_mode)) 
    cout << "File"; 
    else if (S_ISLNK(results.st_mode)) 
    cout << "Symbolic link"; 
    else cout << "File type not recognised"; 
    cout << endl; 

은 내가 t_mode의 파일 모드 비트를 사용해야합니다 알고,하지만 난 방법을 모르는 :

현재,이 같은 파일의 종류를 얻을. See sys/stat.h

답변

7
struct stat results; 

    stat(filename, &results); 

    cout << "Permissions: "; 
    if (results.st_mode & S_IRUSR) 
    cout << "Read permission "; 
    if (results.st_mode & S_IWUSR) 
    cout << "Write permission "; 
    if (results.st_mode & S_IXUSR) 
    cout << "Exec permission"; 
    cout << endl; 
1

소유자 권한 비트는 S_IRWXU부터 <sys/stat.h>까지 제공됩니다. 값은 따라서, 64 (0100 진수)로 곱해질 것이다

cout << "Owner mode: " << ((results.st_mode & S_IRWXU) >> 6) << endl; 

이 기 (S_IRWXG) 및 기타 (S_IRWXO) 유사한 마스크의 이동에있다 0과 7 사이의 값을 출력한다 3 및 0이다. 또한 12 개의 개별 허가 비트에 대해 별도의 마스크가 있습니다.