利用c语言删除目录下文件
最近这段时间工作内容是关于Linux下的简单文件操作,以前对于Linux系统下的文件操作函数都不是太熟悉,经过这次实践,对这些函数使用有了一定的了解。
如何创建文件,读写文件,这些简单的我想大家应该是比较熟悉的,我所介绍的是如何遍历某个目录,并且删除该目录下的文件(可以指定后缀名),并且也可以指定文件的修改时间范围(多少小时以前的旧文件可以删除),下面就是简单的函数实现,仅供初学者参考(毕竟我也是初学者\(^o^)/~)
#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#define FILE_MAX_LEN 256
void rmv_old_files(const char *path, const char *suf, int hours)
{
char filename[FILE_MAX_LEN] = {0};
struct tm *TM;
struct dirent *dirp;
struct stat statbuf;
DIR *dp = NULL;
time_t curr_time;
int nameLen, offset;
char *chTemp = NULL;
curr_time = time((time_t*)NULL);</