linux c读取文件大小,【Linux + C语言】C语言获取文件大小的方法都在这 @@@!!!...

本文介绍了使用C语言在Linux环境下获取文件大小的六种方法:stat(), lstat(), fstat(), lseek(), fseek() 和 ftell() 结合,以及通过shell命令ls。代码已测试并有效,展示了各种方法的使用细节。" 96214693,1342516,词袋模型与n-gram在文本分类中的应用,"['文本处理', '自然语言处理', '机器学习', 'Python', '数据科学']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

笔者在平常项目中常常须要使用C语言求一个文件的大小,特整理了一些经常使用的方法,经过测试代码的形式展现出来,话很少说,直接上代码:shell

#include

#include

#include

#include

#include

#include

#include

#define TEST_FILE"./IMG_3458.JPG"

// call stat() function

static int get_file_size_by_stat(const char *file)

{

int ret;

struct stat file_info;

printf("enter %s() >>>\n", __func__);

ret = stat(file, &file_info);

return (!ret) ? file_info.st_size : -1;

}

// call lstat() function

static int get_file_size_by_lstat(const char *file)

{

int ret;

struct stat file_info;

printf("enter %s() >>>\n", __func__);

ret = lstat(file, &file_info);

return (!ret) ? file_info.st_size : -1;

}

// call fstat() function

static int get_file_size_by_fstat(const char *file)

{

int ret;

int fd;

struct stat file_info;

printf("enter %s() >>>\n", __func__);

fd = open(file, O_RDONLY);

if (fd < 0) {

ret = -1;

perror("open error");

goto exit_entry;

}

ret = fstat(fd, &file_info);

exit_entry:

if (fd >= 0) {

close(fd);

}

return (!ret) ? file_info.st_size : -1;

}

// call lseek() function

static int get_file_size_by_lseek(const char *file)

{

int ret;

int fd;

printf("enter %s() >>>\n", __func__);

fd = open(file, O_RDONLY);

if (fd < 0) {

ret = -1;

perror("open error");

goto exit_entry;

}

ret = lseek(fd, 0, SEEK_END);

exit_entry:

if (fd >= 0) {

close(fd);

}

return ret;

}

// call fseek() and ftell() function

static int get_file_size_by_fseek_and_ftell(const char *file)

{

int ret;

FILE *fp;

printf("enter %s() >>>\n", __func__);

fp = fopen(file, "r");

if (!fp) {

ret = -1;

perror("fopen error");

goto exit_entry;

}

ret = fseek(fp, 0, SEEK_END);

if (ret < 0) {

ret = -1;

perror("fseek error");

goto exit_entry;

}

ret = ftell(fp);

exit_entry:

if (fp) {

fclose(fp);

}

return ret;

}

static int shell_cmd_excute(const char *cmd, char *result, int size)

{

int ret;

FILE *fp;

fp = popen(cmd, "r");

if (!fp) {

ret = -1;

perror("popen error");

goto exit_entry;

}

ret = fread(result, 1, size, fp);

if (ret < 0) {

ret = -1;

perror("fseek error");

goto exit_entry;

}

ret = 0;

exit_entry:

if (fp) {

pclose(fp);

}

return ret;

}

// call shell cmd

static int get_file_size_by_shell_cmd(const char *file)

{

int ret;

char cmd[128];

char result[16];

printf("enter %s() >>>\n", __func__);

snprintf(cmd, sizeof(cmd), "ls -al %s | awk '{print $5}'", file);

printf("shell cmd: %s\n", cmd);

ret = shell_cmd_excute(cmd, result, sizeof(result));

if (!ret && strlen(result)) {

ret = atoi(result);

}

return ret;

}

int main(int argc, const char *argv[])

{

int file_size;

printf("enter %s() >>>\n", __func__);

file_size = get_file_size_by_stat(TEST_FILE);

printf("file_size=%d\n\n\n", file_size);

file_size = get_file_size_by_lstat(TEST_FILE);

printf("file_size=%d\n\n\n", file_size);

file_size = get_file_size_by_fstat(TEST_FILE);

printf("file_size=%d\n\n\n", file_size);

file_size = get_file_size_by_lseek(TEST_FILE);

printf("file_size=%d\n\n\n", file_size);

file_size = get_file_size_by_fseek_and_ftell(TEST_FILE);

printf("file_size=%d\n\n\n", file_size);

file_size = get_file_size_by_shell_cmd(TEST_FILE);

printf("file_size=%d\n\n\n", file_size);

return 0;

}

测试记录以下:windows

被测试文件,在windows下查看大小为:测试

ff3f0be1b8bcc66f08dc08fcde064efd.png

如上测试代码,编译出来,运行结果以下所示,测试证实,全部的获取方法均是有效的。code

67718bf3f04159bf5afeecafc0b14165.png

好了,本次使用C语言获取文件大小的方法就介绍到这里,若是你有更加方便、快捷、高效的方法,也能够在评论席告知,感激涕零。blog

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值