gpt4 book ai didi

c - 检查文件是否存在的最快方法

转载 作者:行者123 更新时间:2023-12-04 12:11:14 24 4
gpt4 key购买 nike

<分区>

我使用的方法只是尝试 fopen() 要检查的文件,

/* --- does file exist??? --- */
char fname[999] = "whatever"; /* constructed during execution */
FILE *fp = NULL; /* try to fopen(fname,"r") */
int isfilefound = 0; /* set true if fopen() succeeds */
if ( (fp = fopen(fname,"r")) /* try to fopen() for read */
!= NULL ) { /* succeeded */
isfilefound = 1; /* set file found flag */
fclose(fp); } /* and just close the file */

是否有更快、资源占用更少的方法?... unix/linux 的特定方法? Windows方式?最好是一种可移植的 posix 兼容方式(大概如上所述)?它被执行了很多次(1000 次),所以我不想无缘无故地打开和关闭文件。

-------------------------------------------- ----------------------
编辑 好的,根据下面的答案,我将以下小功能组合在一起,旨在检查文件(已经:) 是否以 posix、windows 或其他可移植方式存在...

/* ==========================================================================
* Function: isfilexists ( path )
* Purpose: check whether file at path exists
* --------------------------------------------------------------------------
* Arguments: path (I) pointer to null-terminated char string
* containing "path/filename.ext" of
* file whose existence is to be determined
* (path is relative to pwd unless explicitly
* absolute by initial '/' or other syntax)
* --------------------------------------------------------------------------
* Returns: ( int ) 1 if file at path exists, or 0 if not
* --------------------------------------------------------------------------
* Notes: o conditional compiles for various systems,
* depending on whether POSIX or WINDOWS is #define'ed...
* o ...method used:
* 1: use access() on Posix systems,
* 2: PathFileExists() on Windows systems,
* 3: fopen() on any other systems.
* ======================================================================= */
/* --- entry point --- */
int isfilexists ( char *path )
{
/* ---
* allocations and declarations
* ------------------------------- */
int isexists = 0; /* set true if file at path exists */
FILE *fp = NULL; /* fopen() for non-posix,windows */
#define POSIX /* just for testing */
/* ---
* determine whether file at path already exists
* ------------------------------------------------ */
#if defined(POSIX) /* posix-compliant system... */
#include <unistd.h>
if ( access(path,F_OK) == 0 ) /* file at path exists */
isexists = 1; /* so set file exists flag */
#else
#if defined(WINDOWS) /* Windows system... */
isexists = PathFileExists(path); /* set flag if file at path exists */
#else
/* --- fopen() for any other non-posix, non-windows system --- */
if ( (fp = fopen(path,"r")) /* try to fopen() for read */
!= NULL ) { /* succeeded */
isexists = 1; /* set file exists flag */
fclose(fp); } /* and just close the file */
#endif
#endif
return ( isexists ); /* back to caller with 1 if file at path exists */
} /* --- end-of-function isfilexists() --- */

access() 和 fopen() 方法经过测试并且工作正常。无法测试 Windows 的 PathFileExists()。而且我仍然想弄清楚哪些#define'ed 符号可以自动且明确地检查条件编译。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com