檢查檔案或資料夾是否存在
Code:
#include <stdio.h>
#include <unistd.h>
#define BOOL int
#define TRUE 0
#define FALSE -1
#define MAX_LONG_LENGTH 1024
#define MAX_SHORT_LENGTH 128
BOOL is_file_exist_fun(char *filename)
{
int bRet = FALSE;
FILE* fp = fopen(filename, "r");
if (fp)
{
bRet = TRUE;
fclose(fp);
}
return bRet;
}
BOOL is_folder_exist_fun(char *foldername)
{
BOOL bRet = FALSE;
int ret = chdir(foldername);
if (ret == 0)
bRet = TRUE;
return bRet;
}
int main(int argc, char *argv[])
{
BOOL ret = is_file_exist_fun("/etc/exports");
printf("/etc/exports : %s\n", ret==TRUE?"Exist":"Not Exist");
ret = is_folder_exist_fun("/etc/");
printf("/etc/ : %s\n", ret==TRUE?"Exist":"Not Exist");
return 0;
}
Result:
/etc/exports : Exist
/etc/ : Exist
沒有留言:
張貼留言