这篇“怎么用c语言删除某个目录下的文件”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“怎么用c语言删除某个目录下的文件”文章吧。
利用c语言删除目录下文件
如何创建文件,读写文件,这些简单的我想大家应该是比较熟悉的,我所介绍的是如何遍历某个目录,并且删除该目录下的文件(可以指定后缀名),并且也可以指定
文件的修改时间范围(多少小时以前的旧文件可以删除),下面就是简单的函数实现,仅供初学者参考
#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);
    dp = opendir(path);
    if (null == dp)
    {
        return;
    }   
    while((dirp=readdir(dp)) != null)
    {
        if (strcmp(dirp->d_name, ".")==0 || strcmp(dirp->d_name, "..")==0)
        {
            continue;
        }
        namelen = strlen(dirp->d_name);
        chtemp = dirp->d_name;
        if (*suf != '