免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1410 | 回复: 0
打印 上一主题 下一主题

在一个目录下取按修改时间最早的文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-28 22:13 |只看该作者 |倒序浏览
我的程序每次取一个修改时间最早的文件,处理后就会转移这个文件,然后再重新读取一个按修改时间最早的文件。  不知道我这样做是不是有什么问题,或者很笨?  或者有其他好的方法,请不吝赐教。

i feel sad that nobody give me any suggestion. i look like a joke of this forum!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>

char       outfile[257];
void convdate(time_t t, char *Ret_Dt)
{
    char Tdatetime[15] , temp[5];
    struct tm *curtime;

    curtime = localtime(&t);
    memset((char *) Tdatetime , 0 , sizeof(Tdatetime));
    sprintf(temp , "%4d" , 1900 + curtime->tm_year);
    strcat(Tdatetime , temp);
    sprintf(temp , "%02d" , curtime->tm_mon + 1);
    strcat(Tdatetime , temp);
    sprintf(temp , "%02d" , curtime->tm_mday);
    strcat(Tdatetime , temp);
    sprintf(temp , "%02d" , curtime->tm_hour);
    strcat(Tdatetime , temp);
    sprintf(temp , "%02d" , curtime->tm_min);
    strcat(Tdatetime , temp);
    sprintf(temp , "%02d" , curtime->tm_sec);
    strcat(Tdatetime , temp);

    strcpy(Ret_Dt , Tdatetime);
}
int main(int argc, char *argv[])
{

    char     dirname[257];
    int      ret;

    strcpy(dirname,"/u2/wzcj/datas/xianju/ibs/work/err_data");

    ret = GetDirFile(dirname);
    if (ret < 0)
    {
        printf("GetDirFile error\n");
    }
}
int GetDirFile(char *dirname)
{
    DIR           *dir;
    struct dirent *dirp;
    struct stat   statp;
    char          file[257],predate[15],thisdate[15];
    int           i = 0 , j , exist , ret;

    if ( (dir = opendir(dirname)) == NULL)
    {
        return -3;
    }
    while ((dirp = readdir(dir)) != NULL)
    {
      if( strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
         continue ;
      memset((char *)file,0,sizeof(file));
      strcpy(file,dirname);
      strcat(file,"/");
      strcat(file , dirp->d_name);
      if (lstat(file , &statp) < 0)
      {
          printf("lstat %s error\n" , file);
          return -1;
      }
      convdate(statp.st_mtime,thisdate);
      if ( i == 0)
      {
          strcpy(outfile,dirp->d_name);
      }
      else
      {
          if (strcmp(predate,thisdate) <0 )
          {
              strcpy(predate,thisdate);
              strcpy(outfile,dirp->d_name);
          }
      }
      i++;
    }
    closedir( dir ) ;
    return 0;

}

[ 本帖最后由 shine010 于 2006-3-1 14:48 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP