C语言实现获得文件大小,大于某个值,删除该文件
#include <stdio.h>
#define ONE_MB 1024*1024
long get_file_size(char* file_name);
int main(int argc, char *argv[])
{
long length = get_file_size("aaa.txt");
if (length > ONE_MB)
{
unlink("aaa.txt"); // 删除文件
}
return 0;
}
long get_file_size(char* file_name)
{
long length = 0;
FILE *fp = NULL;
fp = fopen(file_name, "rb");
if (fp != NULL)
{
fseek(fp, 0, SEEK_END);
length = ftell(fp);
}
if (fp != NULL)
{
fclose(fp);
fp = NULL;
}
return length;
}