记CC++获取文件大小的几种方法

    xiaoxiao2023-10-07  175

    以下操作需要包含头文件<fstream> | <string> size_t GetFileSize(const std::string& file_name){ std::ifstream in(file_name.c_str()); in.seekg(0, std::ios::end); size_t size = in.tellg(); in.close(); return size; //单位是:byte } 以下操作需要包含头文件<cstdio> | <string> size_t GetFileSize(const std::string& file_name){ FILE* fp = fopen(file_name.c_str(), "r"); fseek(fp, 0, SEEK_END); size_t size = ftell(fp); fclose(fp); return size; //单位是:byte } 以下操作需要包含头文件<sys/stat.h> | <string> size_t GetFileSize(const std::string& file_name){ struct _stat info; _stat(file_name.c_str(), &info); size_t size = info.st_size; return size; //单位是:byte }
    最新回复(0)