C++ 如何获取目录下面的文件/文件的创建时间

转:C++ 如何获取目录下面的文件/文件的创建时间 - 一望无际的南 - 博客园

我手上有一个C++程序会生成一些临时文件,过期以后希望能够删除. 到达成这个目的,我需要做到:

根据提供的路径,

列取所有文件,并比较现在时间跟其创建时间的差,如果超过N天,则删除.

问题分解开来是:

1. 取得某个目录下面所有文件

2. 取得文件的创建日期

3. 取得当前日期跟其创建的日期差

4. 删除文件

为此,我写了一个小程序来测试


// TestFileFunction.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h" 
#include <stdio.h>//remove
#include <io.h>
#include <sys/stat.h>// get file info
#include <time.h>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    printf("Hello World!\n");
    int iresult; 
    struct _stat buf; //in stat head file
    //_FILE__ means the current file. you can assign another file name. IE D:\\test.txt"£©
    iresult = _stat(__FILE__,&buf); 
   //the seconds from Greenwich 1970-1-1 to now.
    printf("seconds of file create-time  from 1970 :%d\n", buf.st_atime);
    //convert to our define format
    //struct tm *localtime(long *clock)
    long* m_sencodes = &buf.st_atime;
    struct tm* m_localTime = localtime(m_sencodes);
    printf("file Local time : %d:%d:%d\n", m_localTime->tm_hour,m_localTime->tm_min,    m_localTime->tm_sec);

    //Get the current time
    long* mptr_currentSeconds = new long;
    time(mptr_currentSeconds);
    printf("current seconds from 1970 :%d\n", *mptr_currentSeconds);
    m_localTime = localtime(mptr_currentSeconds);
    printf("current Local time : %d:%d:%d\n", m_localTime->tm_hour,m_localTime->tm_min, m_localTime->tm_sec);
 
    //compare the two times£¬second
    //double difftime(time_t time2,time_t time1) 
    long diffSeconds = difftime(*mptr_currentSeconds, *m_sencodes);     
    const int SECONDS_OF_DAY= 86400; 
    //how many days?
    int diffDays = diffSeconds/SECONDS_OF_DAY;
    printf("diff time seconds: %d, days: %d\n", diffSeconds, diffDays);
    delete mptr_currentSeconds;
    //ok, now we will list all files in one folder
    struct _finddata_t c_file;
    long hFile;
     
    if ( (hFile = _findfirst("*.*", &c_file)) == -1L )
        printf("No *.* files in current directory");
    else {
        do
        {   //we show files except sub-directory
            if (c_file.attrib != _A_SUBDIR)
            {
                 printf("%s\n", c_file.name);
             }             
         } while ( _findnext(hFile, &c_file) == 0 ) ;   
        _findclose(hFile);
    }

    cin.get();  
    return 0;
}
   

最后附上的是,时间日期函数的参考

<sys/stat.h>

     struct stat {
         dev_t      st_dev;    /* device inode resides on */
         ino_t      st_ino;    /* inode's number */
         mode_t     st_mode;   /* inode's mode */
         nlink_t    st_nlink;  /* number of hard links to the file */
         uid_t      st_uid;    /* user ID of owner */
         gid_t      st_gid;    /* group ID of owner */
         dev_t      st_rdev;   /* device type, for special file inode */
         struct timespec st_atimespec;  /* time of last access */
         struct timespec st_mtimespec;  /* time of last data modification */
         struct timespec st_ctimespec;  /* time of last file status change */
         off_t      st_size;   /* file size, in bytes */
         int64_t    st_blocks; /* blocks allocated for file */
         u_int32_t  st_blksize;/* optimal file sys I/O ops blocksize */
         u_int32_t  st_flags;  /* user defined flags for file */
         u_int32_t  st_gen;    /* file generation number */
     };

时间的转换

┌──────────────────────┐ 
│struct tm                                                     │
│{                                                                │
│ int tm_sec; /*秒,0-59*/                               │
│ int tm_min; /*分,0-59*/                               │
│ int tm_hour; /*时,0-23*/                              │
│ int tm_mday; /*天数,1-31*/                          │
│ int tm_mon; /*月数,0-11*/                           │
│ int tm_year; /*自1900的年数*/                     │
│ int tm_wday; /*自星期日的天数0-6*/             │
│ int tm_yday; /*自1月1日起的天数,0-365*/      │
│ int tm_isdst; /*是否采用夏时制,采用为正数*/   │
│}                                                                │
└──────────────────────┘ 
日期贮存结构date 
┌───────────────┐ 
│struct date                              │
│{                                            │
│ int da_year; /*自1900的年数*/ │
│ char da_day; /*天数*/             │
│ char da_mon; /*月数 1=Jan*/ │
│}                                           │
└───────────────┘ 
时间贮存结构time 
┌────────────────┐ 
│struct time                                 │
│{                                              │
│ unsigned char ti_min; /*分钟*/    │
│ unsigned char ti_hour; /*小时*/  │
│ unsigned char ti_hund;               │
│ unsigned char ti_sec; /*秒*/       │
│                                │
└────────────────┘ 
char *ctime(long *clock) 
本函数把clock所指的时间(如由函time返回的时间)转换成数下列格式的字符串:
Mon Nov 21 11:31:54 1983n 
char asctime(struct tm *tm) 
本函数把指定的tm结构类的时间转换成下列格式的字符串: 
Mon Nov 21 11:31:54 1983n 
double difftime(time_t time2,time_t time1) 
计算结构time2和time1之间的时间差距(以秒为单位) 
struct tm *gmtime(long *clock)
本函数把clock所指的时间(如由函数time返回的时间)转换成格林威治时间,并以tm结构形式返回 
struct tm *localtime(long *clock)
本函数把clock所指的时间(如函数time返回的时间)转换成当地标准时间,并以tm结构形式返回 
void tzset()本函数提供了对UNIX操作系统的兼容性 
long dostounix(struct date *dateptr,struct time *timeptr) 
本函数将dateptr所指的日期,timeptr所指的时间转换成UNIX格式, 并返回自格林威治时间1970年1月1日凌晨起到现在的秒数 
void unixtodos(long utime,struct date *dateptr,struct time *timeptr)
本函数将自格林威治时间1970年1月1日凌晨起到现在的秒数utime转换成DOS格式并保存于用户所指的结构dateptr和timeptr中 
void getdate(struct date *dateblk)
本函数将计算机内的日期写入结构dateblk中以供用户使用 
void setdate(struct date *dateblk)
本函数将计算机内的日期改成由结构dateblk所指定的日期 
void gettime(struct time *timep)
本函数将计算机内的时间写入结构timep中, 以供用户使用 
void settime(struct time *timep)
本函数将计算机内的时间改为由结构timep所指的时间 
long time(long *tloc)
本函数给出自格林威治时间1970年1月1日凌晨至现在所经过的秒数,并将该值存于tloc所指的单元中. 
int stime(long *tp)本函数将tp所指的时间(例如由time所返回的时间)写入计算机中.

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值