码龄10年 暂无认证

34134
访问
1
等级

8
获赞
3
评论

最新评论

  • 20240228阿⑤

    积分乍获得

  • 20231124ziher

    666

  • 20230909八月老师

    什麼時候發佈易語言支持庫呢

一.《WIN32》判断文件夹是否存在

Heart 2023-08-29 09:08:48 发布 308

分类专栏: 开发技术 文章标签: 判断文件夹是否存在

判断文件代码:

///////////////////////////////////////////////////////////////////////Win102
#ifdef WIN32
#include                       //C (Windows)    access
#else
#include                   //C (Linux)      access   
#endif
///////////////////////////////////////////////////////////////////////C++
#include                    //C++           fstream
///////////////////////////////////////////////////////////////////////WIN32
#ifdef WIN32
#include                  //Windows API   FindFirstFile
#include #pragma comment(lib, "shlwapi.lib")  //Windows API   PathFileExists
#endif
///////////////////////////////////////////////////////////////////////boost
#include       //boost         
using namespace std;
 
 
int main()
{
    char file_name[] = "C://迪大学院.txt";
 
    ///////////////////////////////////////////////////////////////////////
    //C     run in windows and linux
    if ( 0 == access(file_name, 0) )                        cout<<"access(): file exist."<<endl;
    else                              cout<<"access(): file not exist."<<endl;
    
    ///////////////////////////////////////////////////////////////////////
    //C++     run in windows and linux
    fstream fs;
    fs.open(file_name, ios::in);
    if (fs)     cout<<"fstream: file exist."<<endl;
    else      cout<<"fstream: file not exist."<<endl;
    fs.close();
       ///////////////////////////////////////////////////////////////////////
    //Windows API     run in windows
#ifdef WIN32
    WIN32_FIND_DATA wfd;
    HANDLE hFind = FindFirstFile(file_name, &wfd);
    if ( INVALID_HANDLE_VALUE != hFind )                              cout<<"FindFirstFile: file exist."<<endl;
    else                                   cout<<"FindFirstFile: file not exist."<<endl; 
    CloseHandle(hFind);
 
    if ( PathFileExists(file_name) )                                cout<<"PathFileExists: file exist."<<endl;
    else                                   cout<<"PathFileExists: file not exist."<<endl;
 
    if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) )    cout<<"GetFileAttributes: file exist."<<endl;
    else                                   cout<<"GetFileAttributes: file not exist."<<endl;
 
    if ( INVALID_HANDLE_VALUE != CreateFile(file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, NULL, NULL) )  cout<<"CreateFile: file exist."<<endl;
    else                                                         cout<<"CreateFile: file not exist."<<endl;
#endif
 
    ///////////////////////////////////////////////////////////////////////
    //boost      run in windows and linux
    boost::filesystem::path path_file(file_name);
    if ( boost::filesystem::exists(path_file) && 
         boost::filesystem::is_regular_file(path_file) )                                             cout<<"boost: file exist."<<endl;
    else                                                   cout<<"boost: file not exist."<<endl;
    
 
    return 0;
}

判断文件夹代码:

////////////////////////////////////////////////////////////win32
#ifdef WIN32
#include                       //C (Windows)    access
#else
#include                           //C (Linux)      access   
#endif
#ifdef WIN32
#include                  //Windows API   FindFirstFile
#include #pragma comment(lib, "shlwapi.lib")      //Windows API   PathFileExists
#endif
////////////////////////////////////////////////////////////boost C++
#include       //boost         
using namespace std;
////////////////////////////////////////////////////////////
 
int main()
{
    char file_name[] = "C:\\迪大学院\\";
 
    ///////////////////////////////////////////////////////////////////////
    //C     run in windows and linux
    if ( 0 == access(file_name, 0) )                        cout<<"access(): path exist."<<endl;
    else                                                    cout<<"access(): path not exist."<<endl;
 
    ///////////////////////////////////////////////////////////////////////
    //Windows API     run in windows
#ifdef WIN32
    WIN32_FIND_DATA wfd;
    HANDLE hFind = FindFirstFile(file_name, &wfd);
    if ( INVALID_HANDLE_VALUE != hFind && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )   cout<<"FindFirstFile: path exist."<<endl;
    else                                                                                                   cout<<"FindFirstFile: path not exist."<<endl;                                                                                        
    CloseHandle(hFind);
 
    if ( PathFileExists(file_name) )                               cout<<"PathFileExists: path exist."<<endl;
    else                                               cout<<"PathFileExists: path not exist."<<endl;
 
    if ( INVALID_FILE_ATTRIBUTES != GetFileAttributes(file_name) )    cout<<"GetFileAttributes: path exist."<<endl;
    else                                   cout<<"GetFileAttributes: path not exist."<<endl;   
#endif
 
    ///////////////////////////////////////////////////////////////////////
    //boost      run in windows and linux
    boost::filesystem::path path_file(file_name);
    if ( boost::filesystem::exists(path_file) && boost::filesystem::is_directory(path_file) )       cout<<"boost: path exist."<<endl;
    else                                                   cout<<"boost: path not exist."<<endl;
    
 
    return 0;
}

比较:

1. 精确判断文件和目录的

     FindFirstFile()            (Windows API,  Windows)

     boost                           (boost,  Windows and Linux)

2. 不精确判断文件夹

      access()                     (C ,  Windows and Linux)

      PathFileExists()         (Windows API ,  Windows)

      GetFileAttributes()     (Windows API,  Windows)

3. 只能判断文件的

      fstream                       (C++ STL,  Windows and Linux)

      CreateFile()                (Windows API,  Windows)


Heart1
0 0 上传作业
X
    网友评论 0条评论 0人参与
    请登陆会员1

    表情

    评论列表

×