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

一、判断文件是否存在

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






  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
山东省基本农田shp文件是指将山东省内的基本农田信息以shp文件的形式进行存储和传输的文件。基本农田是指国家规划确定的农业用地保护的核心区域,具有重要的农业生产和生态功能。 这个shp文件包含了关于山东省基本农田的地理空间数据,如基本农田的边界、面积、位置等信息。它采用矢量数据格式,可以使用GIS软件进行打开和处理。 有了这个shp文件,可以进行一系列的农田规划和管理工作。首先,可以利用shp文件的空间属性,对基本农田进行空间分析和统计,统计山东省基本农田的总面积、分布情况等。根据这些数据,可以制定农田保护政策,合理规划农业用地,保护耕地资源。 其次,基于该shp文件,可以进行土地评价和农田质量分析,了解基本农田的土壤质量、水资源状况、适宜农作物类型等信息,以指导农业生产和土地管理。还可以结合其他数据,如气候数据和经济数据等,进行农田利用和农产品供给的研究与决策。 此外,该shp文件还可以与其它地理信息数据进行叠加分析,如交通网络数据、地形数据等,从而评估基本农田的承载能力、利用潜力及其与其他领域的关联性。 总之,山东省基本农田shp文件是一份具有重要参考价值的数据文件,对于山东省农田资源的保护、合理利用以及农业生产的规划与管理都起到了重要作用。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值