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

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...\n");
}else{
printf("setting file missing,load default..\n");
}
  return ;
}

群里有朋友提起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 ) != - ) {
printf("load custom setting...\n");
} else {
printf("setting file missing,load default..\n");
}
}

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

c语言检测文件是否存在int __cdecl access(const char *, int);的更多相关文章

  1. Linux C语言 检测文件是否存在

    头文件 unistd.h ) { // file exists } else { // file doesn't exist } You can also use R_OK, W_OK, and X_ ...

  2. C语言判断文件夹或者文件是否存在的方法【转】

     C语言判断文件夹或者文件是否存在的方法   方法一:access函数判断文件夹或者文件是否存在 函数原型: int access(const char *filename, int mode); 所 ...

  3. Linux 用C语言判断文件和文件夹

    Linux 用C语言判断文件和文件夹 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #inc ...

  4. linux编程stat检测文件元数据信息

    #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/sta ...

  5. C语言判断文件是否存在(转)

    int   access(const   char   *filename,   int   amode); amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1. 这个函 ...

  6. C语言判断文件是否存在

      用函数access,头文件是io.h,原型:    int   access(const   char   *filename,   int   amode); amode参数为0时表示检查文件的 ...

  7. c语言 判断文件是否存在

    使用access函数 功能: 检查调用进程是否可以对指定的文件执行某种操作. 用法: #include <unistd.h> #include <fcntl.h> int ac ...

  8. 第28月第3天 c语言读写文件

    1. int ConfigIniFile::OpenFile( const char* szFileName ) { FILE *fp; size_t nLen; int nRet; CloseFil ...

  9. linux C之判断文件或目录是否存在 access函数

    http://blog.sina.com.cn/s/blog_6a1837e90100uh5d.html access():判断是否具有存取文件的权限 相关函数    stat,open,chmod, ...

随机推荐

  1. 单节点nginx为两台apache服务器提供负载均衡

    需求:本实验为单节点nginx为两台apache服务器提供负载均衡,所有配置为最简单 1.初始化3台测试server,该关的关了 [root@host101 ~]# vim /etc/hosts 19 ...

  2. CDN的实现原理

    在描述CDN的实现原理,让我们先看传统的未加缓存服务的访问过程,以便了解CDN缓存访问方式与未加缓存访问方式的差别: 用户提交域名→浏览器对域名进行解释→得到目的主机的IP地址→根据IP地址访问发出请 ...

  3. node_nibbler:自定义Base32/base64 encode/decode库

    https://github.com/mattrobenolt/node_nibbler 可以将本源码复制到自己需要的JS文件中,比如下面这个文件,一个基于BASE64加密请求参数的REST工具: [ ...

  4. nodejs初探(四)实现一个多人聊天室

    我们实现的思路是,当有一个人发送过来消息,我们就广播给其他客户端. var net = require('net'); var chatServer = net.createServer(), cli ...

  5. HttpWebRequest.GetResponse 方法 转载

    GetResponse 方法返回包含来自 Internet 资源的响应的 WebResponse 对象. 实际返回的实例是 HttpWebResponse,并且能够转换为访问 HTTP 特定的属性的类 ...

  6. ORA-15028: ASM file '..' not dropped; currently being accessed --转载

    Couple of weeks ago we had a problem with one of our busiest databases. The FRA was filling quite ra ...

  7. BMP图片格式

    BMP图片 BMP采用位映射存储格式,除了图像深度可选以外,不采用其他任何压缩,因此,BMP文件所占用的空间很大.BMP文件的图像深度可选lbit.4bit.8bit及24bit和32bit.BMP文 ...

  8. 如何设置a标签的宽高,如何使a标签的文字垂直居中

    通常情况下a标签是没有宽高的,设置 width 和 height 没有作用. 若要使用 width 和 height,需要把a标签转为块级元素,即:display:block|inline-block ...

  9. 利用Resgen.exe 批量生成resources文件

    Resgen.exe(资源文件生成器)  您可以直接如图操作 转换时在 文本中先写好要转换的文件然后 全选 复制到控制台中 Filename.resx 要转换的文件 ResName1.resource ...

  10. js获取浏览器窗口可视区域大小

    获得浏览器窗口的尺寸(浏览器的视口,不包括工具栏和滚动条)的方法: 一.对于IE9+.Chrome.Firefox.Opera 以及 Safari: •  window.innerHeight - 浏 ...