发新话题
打印

如何在特定目录中搜索某个指定文件

如何在特定目录中搜索某个指定文件

请问如何在特定的目录中搜索某个指定文件?不包括子目录,文件名已知,存在返回真。通过判断_access( "SETATM.HLP", 0 )) != -1是否为真,如果是,则表明在当前目录中SETATM.HLP是存在的代码如下:

#include  
void CSetAtmDlg::OnButtonHelp()
{   LPTSTR lpBuffer="";
    CString lpszHelp;
    ::GetCurrentDirectory(48,lpBuffer);
    lpszHelp=(CString)lpBuffer "\SETATM.HLP";
    if( (_access( "SETATM.HLP", 0 )) != -1 )

{
           ::WinHelp(NULL,lpszHelp,HELP_CONTENTS,0);
}
    else
   ::WinHelp(NULL,"C:\SetAtm\SETATM.HLP",HELP_CONTENTS,0);

}

from MSDN
_access, _waccess
Determine file-access permission.

int _access( const char *path, int mode );

int _waccess( const wchar_t *path, int mode );

Routine Required Header Optional Headers Compatibility
_access   Win 95, Win NT
_waccess  or   Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

Each of these functions returns 0 if the file has the given mode. The function returns ? if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:

EACCES

Access denied: file抯 permission setting does not allow specified access.

ENOENT

Filename or path not found.

Parameters

path

File or directory path

mode

Permission setting

Remarks

When used with files, the _access function determines whether the specified file exists and can be accessed as specified by the value of mode. When used with directories, _access determines only whether the specified directory exists; in Windows NT, all directories have read and write access.

mode Value Checks File For
00 Existence only

02  Write permission
04 Read permission
06 Read and write permission

_waccess is a wide-character version of _access; the path argument to _waccess is a wide-character string. _waccess and _access behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H Routine  _UNICODE

TOP

发新话题