在一些实际的项目中,我们需要去计算某目录下的某个文件的大小,从而继续后续的业务;
如下的方法是各个平台通用的方法,简洁实用,已经验证和测试过:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#ifdef WIN32
#include <windows.h>
#else
#include <unistd.h>
#include <sys/syscall.h>
#ifdef APPLE
#include <sys/mount.h>
#else
#include <sys/statfs.h>
#endif //#ifdef APPLE
#endif //#ifdef WIN32
int64_t GetFileSize(const char * lpFileUrl)
{
struct stat fileStat;
if (stat(lpFileUrl, &fileStat) < 0)
{
return 0;
}
else
{
return fileStat.st_size;
}
}
本文介绍了一种在多个平台上通用的文件大小计算方法,通过使用标准库函数,该方法可以简洁高效地获取指定文件的大小,适用于各种实际项目需求。
:获取文件大小(参数为文件url)&spm=1001.2101.3001.5002&articleId=84307392&d=1&t=3&u=952203503d1942f18d4eeef9f5e6ac3d)
374

被折叠的 条评论
为什么被折叠?



