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

来源:互联网 发布:男朋友体力好体验知乎 编辑:程序博客网 时间:2024/04/27 13:39
#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;}

0 0
原创粉丝点击