linux c 获取文件大小
linux-dash
A beautiful web dashboard for Linux
项目地址:https://gitcode.com/gh_mirrors/li/linux-dash

·
问题描述:
在使用c语言获取文件大小的时候发现有的文件大小可以正确获取,有的不能,以下为初始代码
unsigned long get_file_size(const char *filename)
{
unsigned long size;
FILE* fp = fopen( filename, "rb" );
if(fp==NULL)
{
printf("ERROR: Open file %s failed.\n", filename);
return 0;
}
fseek( fp, SEEK_SET, SEEK_END );
size=ftell(fp);
fclose(fp);
return size;
}
后来发现标准C的文件操作函数不支持对超过2G的文件读取。
解决办法:
使用stat函数可以正确获取到超大文件的状态信息
unsigned long get_file_size(const char *filename)
{
struct stat buf;
if(stat(filename, &buf)<0)
{
return 0;
}
return (unsigned long)buf.st_size;
}




A beautiful web dashboard for Linux
最近提交(Master分支:13 天前 )
186a802e
added ecosystem file for PM2 5 年前
5def40a3
Add host customization support for the NodeJS version 5 年前
更多推荐
所有评论(0)