2009-12-11 5 views
2

ifstream :: open()은 void를 반환하며 파일이 사용 권한으로 인해 열리지 않았는지 확인할 방법이 없습니다. C++에서 현재 프로세스의 파일에 대해 읽기 권한이나 쓰기 권한을 사용할 수 있는지 테스트하는 좋은 API는 무엇입니까? 내가 인식하지 오전파일에 필요한 사용 권한이 있는지 여부를 테스트하는 좋은 방법은 무엇입니까?

struct stat { 
     dev_t  st_dev;  /* ID of device containing file */ 
     ino_t  st_ino;  /* inode number */ 
     mode_t st_mode; /* protection */ 
     nlink_t st_nlink; /* number of hard links */ 
     uid_t  st_uid;  /* user ID of owner */ 
     gid_t  st_gid;  /* group ID of owner */ 
     dev_t  st_rdev; /* device ID (if special file) */ 
     off_t  st_size; /* total size, in bytes */ 
     blksize_t st_blksize; /* blocksize for file system I/O */ 
     blkcnt_t st_blocks; /* number of 512B blocks allocated */ 
     time_t st_atime; /* time of last access */ 
     time_t st_mtime; /* time of last modification */ 
     time_t st_ctime; /* time of last status change */ 
    }; 

:

+1

I/O는 OS에 따라 다르며, OS에 대해 알려 주시면보다 구체적인 답변을 드릴 수 있습니다. –

+0

가능한 중복 : http://stackoverflow.com/questions/1138508/in-c-on-unix-how-can-a-process-tell-what-permissions-it-has-to-a-file-without- op –

+0

이것은 Linux 용입니다. – WilliamKF

답변

2

모드, UID 및 GID를 포함, 정보의 무리를 반환 또한 stat을 사용할 수 있습니다 unistd.h

2

에서 POSIX 액세스() 함수를 사용해보십시오 이 하위 레벨 기능을위한 훌륭한 C++ 래퍼.

1

Windows를 사용하는 경우 GetFileAttributesEx을 사용하여 파일의 속성을 확인할 수 있습니다. 읽기 전용이면 SetFileAttributes를 호출하여 읽기 전용 플래그를 설정 해제해야합니다.

관련 문제