您好,欢迎访问一九零五行业门户网

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
);
其它类似信息

推荐信息