C语言实现获取文件大小与创建修改时间
作者:whik1194
这篇文章主要为大家详细介绍了如何通过C语言实现获取文件大小、创建时间与修改时间,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
源代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <sys/stat.h>
#include <sys/types.h>
#define EXE_ABORT() {system("pause");return 0;}
#define FILEPATH "./test.txt"
int main(int argc, char *argv[])
{
FILE *fp_i;
int i = 0;
int ret = 0;
char buf[128];
struct stat stat_i;
struct tm *tm_p;
ret = stat((const char *)FILEPATH, &stat_i);
if(ret != 0)
{
printf("%s: get file stat failed", FILEPATH);
EXE_ABORT();
}
printf("get file stat success\n");
//! 文件的大小,字节为单位
printf("文件大小: %.02f MB (%d Bytes)\n", stat_i.st_size / 1024.0, stat_i.st_size);
// printf("文件创建时间 : %s", ctime(&stat_i.st_ctime));//Wed Nov 01 19:59:32 2023
// printf("最后一次修改时间: %s", ctime(&stat_i.st_mtime));//Wed Nov 01 19:59:36 2023
// printf("最近一次访问时间: %s", ctime(&stat_i.st_atime));//Wed Nov 01 19:59:36 2023
tm_p = localtime(&stat_i.st_ctime);
memset(buf, 0, sizeof(buf)/sizeof(buf[0])); //2023-11-01 19:59:32
strftime(buf, 128, "%Y-%m-%d %X", tm_p);
printf("文件创建时间 : %s\n", buf);
tm_p = localtime(&stat_i.st_mtime);
memset(buf, 0, sizeof(buf)/sizeof(buf[0]));
strftime(buf, 128, "%Y-%m-%d %X", tm_p);
printf("最后一次修改时间: %s\n", buf);
tm_p = localtime(&stat_i.st_atime);
memset(buf, 0, sizeof(buf)/sizeof(buf[0]));
strftime(buf, 128, "%Y-%m-%d %X", tm_p);
printf("最近一次访问时间: %s\n", buf);
system("pause");
return 0;
}
编译输出:
$ ./a.exe
get file stat success
文件大小: 0.01 MB (10 Bytes)
文件创建时间 : 2023-11-01 19:59:32
最后一次修改时间: 2023-11-01 19:59:36
最近一次访问时间: 2023-11-01 19:59:36
结构体原型:
struct stat
{
dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
ino_t st_ino; /* inode number -inode节点号*/
mode_t st_mode; /* protection -保护模式?*/
nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
uid_t st_uid; /* user ID of owner -user id*/
gid_t st_gid; /* group ID of owner - group id*/
dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/
off_t st_size; /* total size, in bytes -文件大小,字节为单位*/
blksize_t st_blksize; /* blocksize for filesystem I/O -系统块的大小*/
blkcnt_t st_blocks; /* number of blocks allocated -文件所占块数*/
time_t st_atime; /* time of last access -最近存取时间*/
time_t st_mtime; /* time of last modification -最近修改时间*/
time_t st_ctime; /* time of last status change - */
};
方法补充
除了上文的方法,小编还为大家整理了C++获取文件大小的其余方法,希望对大家有所帮助
方法一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
/*确保此文件存在*/
#define TEST_FILE "/home/xxx/mysrc/test/test.file"
/* 获取文件大小的方法:
* 方法1:fseek设置文件指针位置+ftell获取文件指针位置离文件头部的长度(rewind:将文件指针重置到文件头)
* 方法2:使用stat函数,获取文件信息,然后获取文件信息中的长度(stat不适用与符号链接文件;会反馈链接对应的引用件信息) */
int main(){
/*fseek+ftell*/
FILE *fp = NULL;
int iRet = 0;
int fLen = 0;
struct stat fStat;
long int fSize = 0;
fp = fopen( TEST_FILE, "r" );
if( NULL == fp ){
printf( "fopen() error.\n" );
return 0;
}
printf( "fopen() success.\n" );
iRet = fseek( fp, 0, SEEK_END );
if( -1 == iRet ){
fclose( fp );
printf( "fseek() error.\n" );
return 0;
}
printf( "fseek() success.\n" );
fLen = ftell( fp );
if( -1 == fLen ){
fclose( fp );
printf( "ftell() error.\n" );
return 0;
}
printf( "ftell() success.\n" );
printf( "file=%s;len=%d\n", TEST_FILE, fLen );
/*重置文件指针到文件头*/
rewind( fp );
fclose( fp );
/*方法2*/
printf( "==================================================\n" );
memset( &fStat, 0x00, sizeof( fStat ) );
iRet = stat( TEST_FILE, &fStat);
if( -1 == iRet ){
printf( "stat() error.\n" );
return 0;
}
printf( "stat() success.\n" );
fLen = 0;
fLen = fStat.st_size;
printf( "fSize=%d.\n", fLen );
return 1;
}
方法二:
通过C语言文件操作,获取文件大小
以fopen打开的文件,通过fseek可以定位到文件尾,这时使用ftell函数,返回的文件指针偏移值,就是文件的实际大小。
#include <stdio.h>//包含头文件。
int file_size(char* filename)//获取文件名为filename的文件大小。
{
FILE *fp = fopen(filename, "rb");//打开文件。
int size;
if(fp == NULL) // 打开文件失败
return -1;
fseek(fp, 0, SEEK_END);//定位文件指针到文件尾。
size=ftell(fp);//获取文件指针偏移量,即文件大小。
fclose(fp);//关闭文件。
return size;
}到此这篇关于C语言实现获取文件大小与创建修改时间的文章就介绍到这了,更多相关C语言获取文件大小内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
