cpp 获取文件是否存在 获取文件大小
inline bool exists_test0 (const std::string& name) { std::ifstream f(name.c_str()); return f.good(); } inline bool exists_test1 (const std::string& name) { if (FILE *file = fopen(name.c_str(), "r")) { fclose(file); return true; } else { return false; } } inline bool exists_test2 (const std::string& name) { return ( access( name.c_str(), F_OK ) != -1 ); } inline bool exists_test3 (const std::string& name) { //最快 struct stat buffer; return (stat (name.c_str(), &buffer) == 0); } int fileSize(const char *add){ std::ifstream mySource; mySource.open(add, std::ios_base::binary); mySource.seekg(0,std::ios_base::end); int size = mySource.tellg(); mySource.close(); return size; } int get_file_size(std::string filename) // path to file { FILE *p_file = NULL; p_file = fopen(filename.c_str(),"rb"); fseek(p_file,0,SEEK_END); int size = ftell(p_file); fclose(p_file); return size; } long GetFileSize(std::string filename) { struct stat stat_buf; int rc = stat(filename.c_str(), &stat_buf); return rc == 0 ? stat_buf.st_size : -1; } long FdGetFileSize(int fd) { struct stat stat_buf; int rc = fstat(fd, &stat_buf); return rc == 0 ? stat_buf.st_size : -1; }
分类:
C++
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具
2021-10-18 Ubuntu 18.04安装Samba服务器及配置