What's the best way to check if a file exists in C?

后端 未结 9 1311
眼角桃花
眼角桃花 2020-11-22 04:34

Is there a better way than simply trying to open the file?

int exists(const char *fname)
{
    FILE *file;
    if ((file = fopen(fname, \"r\")))
    {
               


        
9条回答
  •  故里飘歌
    2020-11-22 05:03

    You can use realpath() function.

    resolved_file = realpath(file_path, NULL);
    if (!resolved_keyfile) {
       /*File dosn't exists*/
       perror(keyfile);
       return -1;
    }
    

提交回复
热议问题