C語言判斷檔案是否存在,判斷檔案可讀可寫可執行

Koma_Wong發表於2018-09-15
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <io.h>

#define F_OK 0
#define R_OK 2
#define W_OK 4
#define X_OK 6

int main(int argc, char *argv[])
{
	if(argc < 2)
	{
		printf("Usage: ./a.out <filename>\n");
		return -1;
	}
	if(0 == access(argv[1], F_OK))
	{
		printf("%s exist.\n", argv[1]);
	}
	if(0 == access(argv[1], R_OK))
	{
		printf("%s readable.\n", argv[1]);
	}
	if(0 == access(argv[1], W_OK))
	{
		printf("%s writeable.\n", argv[1]);
	}
	if(0 == access(argv[1], X_OK))
	{
		printf("%s executable.\n", argv[1]);
	}
}

執行:

$ gcc a.c
$ ./a.exe a.exe
a.exe exist.
a.exe readable.
a.exe writeable.
a.exe executable.

 

相關文章