在 PHP 函式裡面,有直接 file_exists 可以使用,相當方便:
| 1
2
3
4
5
 | <?php
if(file_exists("files/appleboy.c")) {
    echo "File found!";
}
?>
 | 
在 C 裡面該如何實做?有兩種方式如下:
1. 直接開檔
| 1
2
3
4
5
6
7
8
9
 | bool file_exists(const char * filename)
{
    if (FILE * file = fopen(filename, "r"))
    {
        fclose(file);
        return true;
    }
    return false;
}
 | 
C++ 寫法
| 1
2
3
4
5
6
7
8
 | std::fstream foo;
foo.open("bar");
if(foo.is_open() == true)
     std::cout << "Exist";
else 
     std::cout << "Doesn't Exist";
 | 
2. 讀取檔案狀態
|  1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 | #include<sys/stat.h>
int file_exists (char * fileName)
{
   struct stat buf;
   int i = stat ( fileName, &buf );
     /* File found */
     if ( i == 0 )
     {
       return 1;
     }
     return 0;
}
 | 
See also
- [Linux Kernel] 讀取 /proc 底下資料最佳方法: seq_file interface
- [C/C++] 將字串轉成 16 進位
- [C/C++] cstring (string.h) 函式:strcat, strncat, strcmp, strncmp
- [C/C++] cstring (string.h) 搜尋函式:strstr, strchr
- [網站] 好站連結 (七) Android, javascript, Css, PHP, Perl, FreeBSD, Linux
- [C/C++] count 1 bits of input value by shifting.
- [C/C++] C語言切割字串函式 strsep,分析 URL GET 參數
- [C/C++] strpbrk 在字串中找尋指定的符號或字母
- [C/C++] 切割字串函數:strtok, Network mac address 分割
- [C/C++] 計算二進位任意數含有多少個位元為1?