筆者在日常項目中經常需要使用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;
}
測試記錄如下:
被測試文件,在windows下查看大小為:
如上測試代碼,編譯出來,運行結果如下所示,測試證明,所有的獲取方法均是有效的。
好了,本次使用C語言獲取文件大小的方法就介紹到這里,如果你有更加方便、快捷、高效的方法,也可以在評論席告知,感激不盡。
審核編輯:湯梓紅
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
Linux
+關注
關注
87文章
11345瀏覽量
210392 -
C語言
+關注
關注
180文章
7614瀏覽量
137713 -
文件
+關注
關注
1文章
570瀏覽量
24822
發布評論請先 登錄
相關推薦
linux下c語言編程pdf
linux下c語言編程內容為::基礎知識,進程介紹,文件操作,時間概念,信號處理,消息管理,線程操作,網絡編程,Linux 下
發表于 12-08 10:00
?0次下載
C語言_Linux基本命令與C語言基礎
這篇文章介紹在Linux環境下學習C語言搭建基本的環境過程,了解基礎的幾個命令使用方法,了解Linux下用戶權限配置,標準main函數傳參方
hex文件如何查看原c語言代碼
是處理器可以直接執行的指令,而 C 語言代碼則是人類可讀的高級編程語言代碼。 然而,如果你想要從 .hex 文件中獲取一些有用的信息或者對程
評論