Linux下使用stat函数得到文件大小

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, const char* argv[])
{
    
    
    if(argc < 2)
    {
    
    
        fprintf(stderr, "%s <filename>\n", argv[0]);
        exit(1);
    }
    
    struct stat buf_st;
    int ret = lstat(argv[1], &buf_st);
    if(ret == -1)
    {
    
    
        perror("stat");
        exit(1);
    }

    printf("file size = %d\n", (int)buf_st.st_size);

    return 0;
}


测试结果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/zxy131072/article/details/108534390
今日推荐