牛骨文教育服务平台(让学习变的简单)
博文笔记

c++判断文件是否存在,判断是文件还是目录,获取文件大小,获取程序所在路径

创建时间:2017-04-12 投稿人: 浏览次数:138
#include<io.h>
std::string getSelfPath(){
	char selfPath[1024];
	 GetModuleFileName( NULL, selfPath, 1024 );
	 return selfPath;
}

std::string getSelfExeName(){
	 std::string s = getSelfPath();
	 int pos = s.rfind("\");
	 std::string newS = s.substr(pos + 1, s.length() - pos - 1);
	 return newS;
}
std::string getSelfDir(){
	 std::string s = getSelfPath();
	 int pos = s.rfind("\");
	 std::string newS = s.substr(0, pos);
	 return newS + "\";
}
static long getFileLength(std::string file){
		FILE *fp =NULL;
	    long n = 0;
 
		if ((fp = fopen(file.c_str(), "rb")) == NULL)
	    {
		exit(0);
	    }
	    fseek(fp,0,2);   //指针:移动到文件尾部
	    n = ftell(fp);   //返回指针偏离文件头的位置(即文件中字符个数)
		return n;
}

bool exist(const char * lpPath){
	  /* Check for existence */
   if( (_access( "ACCESS.C", 0 )) != -1 )
   {
		return true;
   }else{
		return false;
   }
}

bool isDir(const char *lpPath){
	return GetFileAttributesA(lpPath)&FILE_ATTRIBUTE_DIRECTORY;
}

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。