C言語:アクセス機能の使用



C Language Use Access Function




1. access()関数は、ユーザーがファイルにアクセスする権限を持っているかどうかを判別する(またはファイルが存在するかどうかを判別する)ために使用されます。


2つ目は、#includeを含める必要がある



3つ、パラメータと戻り値

int access(const char * pathname、int mode)
パラメータ:
パス名:テストするファイルのパスを示します
モード:テストモードの可能な値が次のとおりであることを示します:
R_OK:読み取り権限はありますか
W_OK:書き込み権限があるかどうか
X_OK:実行可能権限はありますか?
F_OK:ファイルは存在しますか?
戻り値:テストが成功した場合は0を返し、そうでない場合は-1を返します。



4、実際のテスト

1.cコードをテストします

#include #include #include /* The access() function is used to determine whether the user has permission to access a file (or to determine whether a file exists). int access(const char *pathname,int mode) Parameters: pathname: indicates the path of the file to be tested mode: indicates that the possible values ​​of the test mode are: R_OK: Do you have read permission W_OK: Do you have write permission X_OK: Does it have executable permissions F_OK: Does the file exist Return value: If the test is successful, return 0, otherwise return -1 */ int main(int argc,char *argv[]){ int rt_value if(argc<2){ printf('Usage:%s filename ',argv[0]) exit(1) } rt_value=access(argv[1],R_OK) if(rt_value==0) printf('File:%s can read rt_value=%d ',argv[1],rt_value) else printf('File:%s can't read rt_value=%d ',argv[1],rt_value) rt_value=access(argv[1],W_OK) if(rt_value==0) printf('File:%s can write rt_value=%d ',argv[1],rt_value) else printf('File:%s can't write rt_value=%d ',argv[1],rt_value) rt_value=access(argv[1],X_OK) if(rt_value==0) printf('File:%s can execute rt_value=%d ',argv[1],rt_value) else printf('File:%s can't execute rt_value=%d ',argv[1],rt_value) rt_value=access(argv[1],F_OK) if(rt_value==0) printf('File:%s exist rt_value=%d ',argv[1],rt_value) else printf('File:%s not exist rt_value=%d ',argv[1],rt_value) return 0 }

2.コンパイルして実行します