您的位置:首页 > 理论基础 > 计算机网络

C语言下,获取文件信息 http://qimo601.iteye.com/blog/1517413

2015-07-24 18:58 633 查看


/article/3766602.html




C语言下,获取文件信息

博客分类:

C/C++

C语言文件信息文件大小

C语言下,如何获取文件的生成时间,日期和文件大小等文件信息。

1、标准库函数并未提供对应的实现,这个Linux上GCC中struct stat的资料

C代码


struct stat {

dev_t st_dev; /* device */

ino_t st_ino; /* inode */

mode_t st_mode; /* protection */

nlink_t st_nlink; /* number of hard links */

uid_t st_uid; /* user ID of owner */

gid_t st_gid; /* group ID of owner */

dev_t st_rdev; /* device type (if inode device) */

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 change */

};

实现:

C代码


int fd,size;

struct stat buf;

fd=open("file",O_RDONLY);

fstat(fd,&buf);

size=buf.st_size;

2、也可以这样直接得到文件的大小

C代码


// 将指针定位到文件末尾

fseek( pFile, 0L, SEEK_END );

//告诉当前指针位置,Byte

fileSize = ftell( pFile );

3、可以通过Qt中QFileInfo很方便就获取到文件信息

Cpp代码


//打开文件

QFileInfo f("./test.txt");

//获取文件创建时间

QDateTime createTime=f.created();

//获取文件最后更新时间

QDateTime updateTime=f.lastModified();

//获取文件最后浏览时间

QDateTime lastRTime=f.lastRead();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: