许多小伙伴在Linux下运行下位机程序时,会通过日志文件检测运行状态,当日志文件过多时,需要定期的清理,可怎么清空呢?
最常用的方式就是通过日志文件的日期和文件大小为参考进行清理,这就会设置到一个函数readdir,运用该函数可以遍历文件夹下的文件信息,从而实现效果。
首先,需要用的头文件如下:
《----代码1-------》
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h> //很重要
#include<dirent.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
//补充一个知识点----------------------------------------------------------------------
这里的#include <sys/stat.h>
文件状态的头文件
是unix/linux系统定义文件状态所在的伪标准头文件。
含有类型与函数:
dev_t st_dev Device ID of device containing file.
ino_t st_ino File serial number.
mode_t st_mode Mode of file (see below).
nlink_t st_nlink Number of hard links to the file.
uid_t st_uid User ID of file.
gid_t st_gid Group ID of file.
dev_t st_rdev Device ID (if file is character or block special).
off_t st_size For regular files, the file size in bytes.
For symbolic links, the length in bytes of the
pathname contained in the symbolic link.
For a shared memory object, the length in bytes.
For a typed memory object, the length in bytes.
For other file types, the use of this field is
unspecified.
time_t st_atime Time of last access.
time_t st_mtime Time of last data modification.
time_t st_ctime Time of last status change.
int chmod(const char *, mode_t);
int fchmod(int, mode_t);
int fstat(int, struct stat *);
int lstat(const char *restrict, struct stat *restrict);
int mkdir(const char *, mode_t);
int mkfifo(const char *, mode_t);
int mknod(const char *, mode_t, dev_t);
int stat(const char *restrict, struct stat *restrict);
mode_t umask(mode_t);
使用stat函数最多的可能是ls-l命令,用其可以获得有关一个文件的所有信息。
一般头文件在/usr/include下面,这里是标准C程序头文件,如果你的头文件前加了 <sys/*>,那说明这是系统调用函数头文件,其在/usr/include/sys下面。
函数都是获取文件(普通文件,目录,管道,socket,字符,块()的属性。函数原型#include <sys/stat.h>
int stat(const char *restrict pathname, struct stat *restrict buf);提供文件名字,获取文件对应属性。
int fstat(int filedes, struct stat *buf);通过文件描述符获取文件对应的属性。
int lstat(const char *restrict pathname, struct stat *restrict buf);连接文件描述命,获取文件属性。
文件对应的属性struct stat {
mode_t st_mode; //文件对应的模式,文件,目录等
ino_t st_ino; //inode节点号
dev_t st_dev; //设备号码
dev_t st_rdev; //特殊设备号码
nlink_t st_nlink; //文件的连接数
uid_t st_uid; //文件所有者
gid_t st_gid; //文件所有者对应的组
off_t st_size; //普通文件,对应的文件字节数
time_t st_atime; //文件最后被访问的时间
time_t st_mtime; //文件内容最后被修改的时间
time_t st_ctime; //文件状态改变时间
blksize_t st_blksize; //文件内容对应的块大小
blkcnt_t st_blocks; //伟建内容对应的块数量
};
//补充完毕----------------------------------------------------------------------》》》》》
继续,定义一个文件信息的结构
《----代码2-------》
typedef struct file_list
{
time_t file_ctime; //文件的日期
char* file_name; //文件名
struct file_list* next; //文件的指针
}file_list_t;
接下来,就是文件操作过程,
DIR* dir;
struct dirent* ptr;
int i;
int flag = 0;
struct stat file_stat;
char* dir_path;
file_list_t* file_list_head = NULL;
file_list_t* file_list_tail = NULL;
file_list_t* file_list_atmp = NULL;
file_list_t* file_list_btmp = NULL;
file_list_t* file_list_node = NULL;
dir_path = (char*)malloc(256); //文件夹路径的空间开辟成256个字节,注意如果实际使用时路径并不长,可以改成128即可
memset(dir_path, 0, 256);
strcpy(dir_path, argv[1]);
dir = opendir(dir_path);
while((ptr = readdir(dir)) != NULL) //调用readdir函数,查找文件指针,实现循环遍历文件
{
memset(dir_path, 0, 256);
strcpy(dir_path, argv[1]);
strcat(dir_path, "/");
strcat(dir_path, ptr->d_name);
stat(dir_path, &file_stat);//获取到文件的信息
if(S_ISREG(file_stat.st_mode))//S_ISREG是否是一个常规文件
{
file_list_node = (file_list_t*)malloc(sizeof(file_list_t));
file_list_node->file_name = (char*)malloc(256);
memset(file_list_node->file_name, 0, 256);
file_list_node->next = NULL;
file_list_node->file_ctime = file_stat.st_ctime;//拷贝到文件的日期
strcpy(file_list_node->file_name, ptr->d_name);//拷贝到文件名称
if(file_list_head == NULL)//空指针检测
{
file_list_head = file_list_node;
file_list_tail = file_list_node;
}
else
{
flag = 0;
file_list_atmp = file_list_head;
file_list_btmp = NULL;
while(file_list_atmp != NULL)
{//文件排序操作
if(file_list_node->file_ctime < file_list_atmp->file_ctime)
{
flag = 1; //时间判断标志
if(file_list_btmp == NULL)
{
file_list_btmp = file_list_head;
file_list_head = file_list_node;
file_list_head->next = file_list_btmp;
}
else
{
file_list_btmp->next = file_list_node;
file_list_node->next = file_list_atmp;
}
break;
}
file_list_btmp = file_list_atmp;
file_list_atmp = file_list_atmp->next; //指向下一个文件
}
if (flag == 0)
{
file_list_tail->next = file_list_node;
file_list_tail = file_list_node;
}
}
}
}
closedir(dir);//关闭文件
file_list_atmp = file_list_head;
//将排序好的文件打印一下,验证结果
while(file_list_atmp != NULL)
{
printf("c_time:%ld file_name:%-s \n", file_list_atmp->file_ctime, file_list_atmp->file_name);
file_list_btmp = file_list_atmp;
file_list_atmp = file_list_atmp->next;
free(file_list_btmp->file_name);
free(file_list_btmp);
}
free(dir_path);//释放空间
}
以上就是全部操作,小伙伴学习到了吗?至于Windows的文件排序,等下次更新哦~