筆者在日常項(xiàng)目中經(jīng)常需要使用C語言求一個(gè)文件的大小,特整理了一些常用的方法,通過測(cè)試代碼的形式展示出來,話不多說,直接上代碼:
#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;
}
測(cè)試記錄如下:
被測(cè)試文件,在windows下查看大小為:

如上測(cè)試代碼,編譯出來,運(yùn)行結(jié)果如下所示,測(cè)試證明,所有的獲取方法均是有效的。

好了,本次使用C語言獲取文件大小的方法就介紹到這里,如果你有更加方便、快捷、高效的方法,也可以在評(píng)論席告知,感激不盡。
-
Linux
+關(guān)注
關(guān)注
87文章
11459瀏覽量
212782 -
C語言
+關(guān)注
關(guān)注
180文章
7630瀏覽量
140311 -
文件
+關(guān)注
關(guān)注
1文章
578瀏覽量
25201
發(fā)布評(píng)論請(qǐng)先 登錄
C語言-文件編程
Linux操作系統(tǒng)-C語言編程入門-pdf

linux下c語言編程pdf
C語言教程之獲取當(dāng)前日期與時(shí)間
C語言教程之獲取Caps Lock鍵狀態(tài)
Linux下C語言編程入門教程詳細(xì)說明

C語言_Linux基本命令與C語言基礎(chǔ)
深入探索Linux中的C語言
C語言獲取文件長(zhǎng)度的兩種方法

評(píng)論