VC判断文件目录是否存在
原文地址::http://blog.csdn.net/pgshow/article/details/7684822 相关网帖 1、VC中 判断 目录 , 文件 是否 存在 ,创建 目录 的方法 ----http://www.cnblogs.com/kiddo/archive/2007/09/03/879950.html 1. 使用_ access 函数,函数原型为 int _access( const
原文地址::http://blog.csdn.net/pgshow/article/details/7684822
相关网帖
1、VC中判断目录,文件是否存在,创建目录的方法 ----http://www.cnblogs.com/kiddo/archive/2007/09/03/879950.html
1. 使用_access函数,函数原型为 int _access( const char *path, int mode );
2. 使用CreateFile函数,函数原型为:
HANDLE CreateFile( LPCTSTR lpFileName, // pointer to name of the file DWORD dwDesiredAccess, // access (read-write) mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes DWORD dwCreationDisposition,
// how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile // handle to file with attributes to // copy );
3. 使用FindFirstFile函数,函数原型为: HANDLE FindFirstFile( LPCTSTR lpFileName, // pointer to name of file to search for LPWIN32_FIND_DATA lpFindFileData // pointer to returned information );
//例子:
BOOL CPubFunc::DirectoryExist(CString Path)
{
WIN32_FIND_DATA fd;
BOOL ret = FALSE;
HANDLE hFind = FindFirstFile(Path, &fd);
if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{ //目录存在
ret = TRUE;
}
FindClose(hFind);
return ret;
}
4. 使用GetFileAttributes函数,函数原型如下: DWORD GetFileAttributes( LPCTSTR lpFileName // pointer to the name of a file or directory );
5. 使用Shell Lightweight Utility APIs函数
PathFileExists()专门判断文件和目录时否存在的函数文件名可读性比较强还可以判断目录是否存在 Header: Declared in Shlwapi.h Import Library: Shlwapi.lib 以上的各种方法供参考,函数具体用法需参见MSDN。
//这是MSDN中的例子:
#include
#include
#include "Shlwapi.h"
void main( void )
{
// Valid file path name (file is there).
char buffer_1[] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;
// Invalid file path name (file is not there).
char buffer_2[] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;
// Return value from "PathFileExists".
int retval;
// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout
cout
cout
}
else{
cout
cout
}
// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout
cout
cout
}
else{
cout
cout
}
}
6.使用CFileFind 类....这是一个InternetServices类,在此可以借用一下。也可以用于遍历文件夹(s可指定深度)
BOOL CPubFunc::FileExist(CString FileName)
{
CFileFind fFind;
return fFind.FindFile(FileName);
}
//创建目录
#include
BOOL CreateDirectory(
LPCTSTR lpPathName,
// pointer to directory path string
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security descriptor
);
1. 使用_access函数,函数原型为 int _access( const char *path, int mode );
2. 使用CreateFile函数,函数原型为:
HANDLE CreateFile( LPCTSTR lpFileName, // pointer to name of the file DWORD dwDesiredAccess, // access (read-write) mode DWORD dwShareMode, // share mode LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security attributes DWORD dwCreationDisposition,
// how to create DWORD dwFlagsAndAttributes, // file attributes HANDLE hTemplateFile // handle to file with attributes to // copy );
3. 使用FindFirstFile函数,函数原型为: HANDLE FindFirstFile( LPCTSTR lpFileName, // pointer to name of file to search for LPWIN32_FIND_DATA lpFindFileData // pointer to returned information );
//例子:
BOOL CPubFunc::DirectoryExist(CString Path)
{
WIN32_FIND_DATA fd;
BOOL ret = FALSE;
HANDLE hFind = FindFirstFile(Path, &fd);
if ((hFind != INVALID_HANDLE_VALUE) && (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{ //目录存在
ret = TRUE;
}
FindClose(hFind);
return ret;
}
4. 使用GetFileAttributes函数,函数原型如下: DWORD GetFileAttributes( LPCTSTR lpFileName // pointer to the name of a file or directory );
5. 使用Shell Lightweight Utility APIs函数
PathFileExists()专门判断文件和目录时否存在的函数文件名可读性比较强还可以判断目录是否存在 Header: Declared in Shlwapi.h Import Library: Shlwapi.lib 以上的各种方法供参考,函数具体用法需参见MSDN。
//这是MSDN中的例子:
#include
#include
#include "Shlwapi.h"
void main( void )
{
// Valid file path name (file is there).
char buffer_1[] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;
// Invalid file path name (file is not there).
char buffer_2[] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;
// Return value from "PathFileExists".
int retval;
// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout
cout
cout
}
else{
cout
cout
}
// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout
cout
cout
}
else{
cout
cout
}
}
6.使用CFileFind 类....这是一个InternetServices类,在此可以借用一下。也可以用于遍历文件夹(s可指定深度)
BOOL CPubFunc::FileExist(CString FileName)
{
CFileFind fFind;
return fFind.FindFile(FileName);
}
//创建目录
#include
BOOL CreateDirectory(
LPCTSTR lpPathName,
// pointer to directory path string
LPSECURITY_ATTRIBUTES lpSecurityAttributes // pointer to security descriptor
);

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

出現0x80004005錯誤代碼怎麼辦 小編教你0x80004005錯誤代碼解決方法

hiberfil.sys是什麼檔案? hiberfil.sys可以刪除嗎?
