关于判断文件是否存在最高效的函数(转)

80 篇文章 2 订阅
7 篇文章 0 订阅

判断文件存在方法有很多,例如CreateFile,FindFirstFile,GetFileAttributes,PathFileExists等等。但是哪一种更加高效呢?其实作为常识,可能都能判断出GetFileAttributes和PathFileExists会比较快(而实际上PathFileExists就是调用的GetFileAttributes)。

下面是google一份开源代码中提到的统计结果

// NOTE: This is the fastest implementation I found. The results were:
// CreateFile 1783739 avg ticks/call
// FindFirstFile 634148 avg ticks/call
// GetFileAttributes 428714 avg ticks/call
// GetFileAttributesEx 396324 avg ticks/call

为什么会这样呢?大概了看了下,原因应该是这样的。

1.CreateFile会创建句柄,需要一个完整IO流程,所以需要的时间比如非常长。
2.FindFirstFile回去查询文件夹的文件,虽然不会真正的打开文件句柄,并且在文件已经被缓存的情况下,走的是fastio流程,所以查询时间大幅下降,但是操作略微繁琐,导致他不是最好的选择。
3. GetFileAttributes 和GetFileAttributesEx 也设置了QueryOnly标志,不需要获得真正的句柄,并且能够走fastio流程,也没有文件夹查询等工作,所以速度最快。

那么为什么GetFileAttributesEx 会快那么一点点呢?因为这个函数少了一个获取BasicInformation,也就是少了一个fastio流程。所以速度更快。这样看来,自己实现一个PathFileExistsEx效率可以高过PathFileExists了。(其实没多大实际意义)

google就是这样做的:

bool File::Exists(const TCHAR* file_name) { 
	ASSERT1(file_name && *file_name); 
	ASSERT1(lstrlen(file_name) > 0);

	// NOTE: This is the fastest implementation I found. The results were: 
	// CreateFile 1783739 avg ticks/call 
	// FindFirstFile 634148 avg ticks/call 
	// GetFileAttributes 428714 avg ticks/call 
	// GetFileAttributesEx 396324 avg ticks/call 
	WIN32_FILE_ATTRIBUTE_DATA attrs = {0}; 
	return 0 != ::GetFileAttributesEx(file_name, ::GetFileExInfoStandard, &attrs); 
}


转自:http://0cch.net/wordpress/?p=84


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux C中,判断文件是否存在的方法有很多种,以下是几种高效的方法: 1. access函数 access函数可以检查文件或目录是否存在,并且可以检查文件或目录是否有指定的权限。如果文件或目录不存在,则access函数会返回-1,errno被设置为ENOENT。 示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> int main(int argc, char **argv) { if (access("/path/to/file", F_OK) == -1) { if (errno == ENOENT) { printf("File does not exist.\n"); } else { perror("access"); } return EXIT_FAILURE; } printf("File exists.\n"); return EXIT_SUCCESS; } ``` 2. stat函数 stat函数可以获取文件或目录的详细信息,如果文件或目录不存在,则stat函数会返回-1,errno被设置为ENOENT。 示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <errno.h> int main(int argc, char **argv) { struct stat st; if (stat("/path/to/file", &st) == -1) { if (errno == ENOENT) { printf("File does not exist.\n"); } else { perror("stat"); } return EXIT_FAILURE; } printf("File exists.\n"); return EXIT_SUCCESS; } ``` 3. fopen函数 fopen函数可以打开文件,如果文件存在,则fopen函数会返回NULL,并且errno被设置为ENOENT。 示例代码: ``` #include <stdio.h> #include <stdlib.h> #include <errno.h> int main(int argc, char **argv) { FILE *fp; if ((fp = fopen("/path/to/file", "r")) == NULL) { if (errno == ENOENT) { printf("File does not exist.\n"); } else { perror("fopen"); } return EXIT_FAILURE; } fclose(fp); printf("File exists.\n"); return EXIT_SUCCESS; } ``` 以上是几种高效判断文件是否存在的方法,可以根据实际情况选择使用。其中,access函数是最简单的方法,stat函数可以获取更多的文件信息,而fopen函数可以直接打开文件进行读写操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值