- 论坛徽章:
- 0
|
我的程序每次取一个修改时间最早的文件,处理后就会转移这个文件,然后再重新读取一个按修改时间最早的文件。 不知道我这样做是不是有什么问题,或者很笨? 或者有其他好的方法,请不吝赐教。
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 编辑 ] |
|