我想读取C目录中所有文件的统计信息(Linux:Fedora)
我已经声明了这种结构:
struct stat st = {0};
然后,我检查目录是否存在。
if(stat("/home/gadre/Source",&st) == -1)
{
status = mkdir("/home/gadre/Source", 0777);
}
syslog(LOG_INFO, "Source Directory stage completed\n");
stat是:
struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
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 ID (if special file) */
off_t st_size; /* total size, in bytes */
blksize_t st_blksize; /* blocksize for file system I/O */
blkcnt_t st_blocks; /* number of 512B 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 */
};
现在,一旦我进入目录,我将要检查每个文件的最后修改时间st_mtime。
任何想法我应该使用哪种数据结构...应该先将fd存储在列表中,然后在其上进行迭代检查...什么是有效的方法。
谢谢。
通用方法是循环播放,没有任何列表,例如容器,
dp = opendir(fullpath))
并获取目录指针dp
像这样循环阅读while ( (dirp = readdir(dp)) != NULL )
dirp->d_name
filepath = fullpath + "/" + dirp->d_name
lstat
以获取时间戳信息PS我更喜欢使用,lstat
因为目录中的文件之一可能是符号链接,在这种情况下,lstat
它将返回符号链接本身的时间戳,而不是它指向的文件的时间戳。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句