c语言检测文件是否存在int __cdecl access(const char *, int);

最近写代码,遇到很多地方需要判断文件是否存在的。网上的方法也是千奇百怪,“百家争鸣”。

fopen方式打开的比较多见,也有其他各种方式判断文件是否存在的,由于其他方法与本文无关,所以不打算提及。

笔者近来使用winapi比较多,于是顺便搜索了msdn,找到了一个函数:PathFileExists

BOOL PathFileExists(
  _In_ LPCTSTR pszPath
);
以下是笔者最初的方法,windows api原则上提供的函数应该是最合理高效的,起码这个方法在windows平台上来说有足够的官腔味。
/*Minimum supported client:    Windows 2000 Professional, Windows XP [desktop apps only]
Minimum supported server:    Windows 2000 Server [desktop apps only]
DLL:                        Shlwapi.dll (version 4.71 or later)

*/
#include <Windows.h>                    //Windows API   FindFirstFile 
#include <Shlwapi.h>                    //Windows API   PathFileExists
#pragma comment(lib, "shlwapi.lib")

int main(void)
{
    if(PathFileExists("setting.ini")){
        printf("load custom setting...
");
    }else{
        printf("setting file missing,load default..
");
    }
  return 0;
}

群里有朋友提起fopen是posix系统中打开文件fd的标准open接口。笔者也曾疑虑,如果一个文件几十个G,会不会在fopen的过程中占用很多资源,

后来求证得到的回复:

肯定不会加载啊
正如你说的,如果文件 xxxG 怎么办

文件是用指针访问的,所以不用担心你的问题

由于笔者目前研究范围和能力有限,没去研究。

笔者的程序有时候需要兼容linux,通常会在cygwin下面编译并测试,也是再三觉得,过于依赖于特定操作系统的api有点不妥。

在google上搜索了了一下,stackoverflow给出了一个答案:

What's the best way to check if a file exists in C? (cross platform)

使用函数

int __cdecl access(const char *, int);

笔者常用的tcc,Visual c++ 6.0,gcc 打开tcc-win32-0.9.26查看相关引用头文件时发现unistd.h只是简单的#include了一次io.h

于是大胆的猜想了一下,Visual c++ 6.0的头文件目录里可能有io.h文件,打开看了一下,果然有,也有相关的函数申明。

根据stackoverflow的方法,笔者写了一段兼容代码,以下代码支持Linux-gcc,TCC(windows),Visual C++6.0,Mingw-gcc(Cygwin)

#include <stdio.h>
#if defined(WIN32) || defined(_WIN32) || defined(WIN64)|| defined(_WIN64)
    #include <io.h>
    #ifndef F_OK
        #define    F_OK    0    /* Check for file existence */
    #endif
#endif
#if defined(__CYGWIN__)|| defined(__linux__)|| defined(linux) || defined(__linux)
#include <unistd.h>
#endif

int main(void)
{
    if( access( "setting.ini", F_OK ) != -1 ) {
        printf("load custom setting...
");
    } else {
         printf("setting file missing,load default..
");
    }
}

至此,妈妈再也不用担心我不会确认文件是否存在了 ;)

原文地址:https://www.cnblogs.com/passedbylove/p/4627471.html