如题目所示,如何判断某个路径是否存在?

解决方案 »

  1.   

    int _access( const char *path, int mode );int _waccess( const wchar_t *path, int mode );pathFile or directory pathmodePermission settingmode Value    Checks File For 
    00            Existence only 
    02            Write permission 
    04            Read permission 
    06            Read and write permission 
      

  2.   

    Return ValueEach of these functions returns 0 if the file has the given mode. The function returns –1 if the named file does not exist or is not accessible in the given mode; in this case, errno is set as follows:EACCESAccess denied: file’s permission setting does not allow specified access.ENOENTFilename or path not found.
      

  3.   

    #include <windows.h>
    #include <iostream.h>
    #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 << "Search for the file path of : " << lpStr1 << endl;
        cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
        cout << "The return from function is : " << retval << endl;
    }else
    {
        cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
        cout << "The return from function is : " << retval << endl;
    }// Search for the presence of a file with a false result.
    retval = PathFileExists(lpStr2);if(retval == 1)
    {
        cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
        cout << "Search for the file path of : " << lpStr2 << endl;
        cout << "The return from function is : " << retval << endl;
    }
    else
    {
        cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
        cout << "The return from function is : " << retval << endl;
    }}
      

  4.   

    可是编译的时候提示“Shlwapi.h”文件找不到呀。
      

  5.   

    安装个vsp6.0,就是vc studio的补丁!
      

  6.   

    检查指定文件夹是否存在:PathIsDirectory()
    方法一:
    检查给定路径是否根目录:BOOL PathIsRoot(LPCTSTR pPath);
    说明:Returns TRUE for paths such as “\”, “ X:\”, “\\ server\ share”, or “\\ server\”。Paths such as “..\path2” will return FALSE.
         用这两个函数要先:#include <shlwapi.h>;
             再把这个文件加入工程:shlwapi.lib
    方法二:
    GetFileAttributes检查文件是否存在,并且检查是否文件夹属性FILE_ATTRIBUTE_DIRECTORY
    ----------------------------------------------------------
    DWORD = GetFileAttributes(_T("f:\\win98"));
    if(dwAttr != 0xFFFFFFFF && (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
    cout<<"exist"<<endl;
    else
    cout<<"NOT exist"<<endl;
    ----------------------------------------------------------
    方法三:
    用下面第113条的_access函数同样可以
    ----------------------------------------------------------
    if(_access("f:\\win98",0)!=-1)
    cout<<"exist"<<endl;
    else
    cout<<"NOT exist"<<endl;
    ----------------------------------------------------------
    方法四:
    用PathFileExists函数,见MSDN介绍,需要的条件同方法一。
    另外,Shlwapi.h及Shlwapi.lib好象是安装PlatForm SDK后才有的文件。
      

  7.   

    用方法三,即_access函数记得先包含头文件#include <io.h>。
      

  8.   

    GetFileAttributes()或者用FindFirstFile()
      

  9.   

    PathFileExists(...)注意:
    #include <shlwapi.h>                // 找不到建议你重装VC
    #pragma comment(lib, "shlwapi.lib")