Thứ Tư, 15 tháng 5, 2013

[C/C++] Check if a file exists or not



you can use fopen("file_name", "w+"); to check if a file exist or not.
For more checking option, such as check access level you can use  access(), e.g.:
#include <unistd.h>

int res = access(path, R_OK);
if (res < 0) {
    if (errno == ENOENT) {
         // file does not exist
    } else if (errno == EACCES]) {
         // file exists but is not readable
    } else {
         // uh oh
    }
}

Không có nhận xét nào:

Đăng nhận xét