现在的位置: 首页 > 综合 > 正文

判断文件、目录是否存在:C、C++、Windows API、 boost

2018年10月04日 ⁄ 综合 ⁄ 共 3137字 ⁄ 字号 评论关闭

一、判断文件是否存在

#ifdef WIN32
#include <io.h>                      //C (Windows)    access
#else
#include <unistd.h>                  //C (Linux)      access   
#endif

#include <fstream>                   //C++           fstream

#ifdef WIN32
#include <Windows.h>                 //Windows API   FindFirstFile
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")  //Windows API   PathFileExists
#endif

#include <boost/filesystem.hpp>      //boost         
using namespace std;




int main()
{
	char file_name[] = "D://aa.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;
}

二、判断目录是否存在

#ifdef WIN32
#include <io.h>                      //C (Windows)    access
#else
#include <unistd.h>                  //C (Linux)      access   
#endif

#ifdef WIN32
#include <Windows.h>                 //Windows API   FindFirstFile
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")  //Windows API   PathFileExists
#endif

#include <boost/filesystem.hpp>      //boost         
using namespace std;




int main()
{
	char file_name[] = "D://b";


	//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)

抱歉!评论已关闭.