直接上代码:
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "fstream"
#include <dirent.h>
#include <string>
#include <string.h>
#include <unistd.h>
#include <vector>
#define fileNumber 4000
using namespace std;
int main( void )
{
/* struct stat buf;
int result;
//获得文件状态信息
result =stat( "/home/jjkj/CTdevice", &buf );
//显示文件状态信息
if( result != 0 )
perror( "显示文件状态信息出错" );//并提示出错的原因,如No such file or directory(无此文件或索引)
else
{
printf("文件大小: %d\n", buf.st_size);
printf("文件创建时间: %s", ctime(&buf.st_ctime));
printf("访问日期: %s", ctime(&buf.st_atime));
printf("最后修改日期: %s", ctime(&buf.st_mtime));
}*/
struct stat buf;
int result;
printf("1");
vector<long> fileCTime;
printf("2");
//long fileCTime[2000];
int j;
for(j=0;j<fileNumber;j++)
{
// fileCTime[j]=0;
fileCTime.push_back(0);
}
j=0;
DIR * dir;
struct dirent * ptr;
// char file_list[100][40];
int i=0;
char srcFile1[fileNumber][100];
// string rootdirPath = "/home/jjkj/CTdevice";
string rootdirPath = "/home/jjkj/data";
char * mid;
char time[25];
//char timer[9];
char* timer=new char[9];
string x;
string dirPath;
const char* constc = nullptr; //初始化const char*类型,并赋值为空
dir = opendir((char *)rootdirPath.c_str()); //打开一个目录
while((ptr = readdir(dir)) != NULL) //循环读取目录数据
{
printf("***********************************************************************************************\n");
printf("d_name : %s\n", ptr->d_name); //输出文件名
x=ptr->d_name;
dirPath = rootdirPath+"/" + x;
constc= dirPath.c_str();
result =stat( constc, &buf );
printf("d_path : %s\n", dirPath.c_str()); //输出文件绝对路径 //
if( result != 0 )
perror( "显示文件状态信息出错" );//并提示出错的原因,如No such file or directory(无此文件或索引)
else
{
printf("文件大小: %d\n", buf.st_size);
printf("文件创建时间: %s\n", ctime(&buf.st_ctime));
mid=ctime(&buf.st_ctime);
strcpy(time,mid);
timer[0]=time[8];
timer[1]=time[9];
timer[2]=time[11];
timer[3]=time[12];
timer[4]=time[14];
timer[5]=time[15];
timer[6]=time[17];
timer[7]=time[18];
timer[8]='\0';
// const char *nptr=timer;
long intTime=atol(timer);//days hours minutes seconds
fileCTime[j]=intTime;
printf("文件创建时间: %ld\n",intTime);
//printf("访问日期: %s", ctime(&buf.st_atime));
//printf("最后修改日期: %s", ctime(&buf.st_mtime));
}
//printf("***********************************************************************************************\n");
x = dirPath.c_str();
strcpy(srcFile1[i],dirPath.c_str()); //存储到数组
j++;
if ( ++i>=fileNumber )
{
break;
}
}
delete [] timer;
closedir(dir);//关闭目录指针
printf("the j is %d\n",j);
printf("the i is %d\n",i);
long t;
char ch[100];
// for(int ii=0;ii<i;ii++)
// {
// printf("the time is: %d\n",fileCTime[ii]);
// printf("the path is: %s\n", srcFile1[ii]);
// }
for(int ii=0;ii<i-1;ii++)
{
for(int jj=0;jj<i-ii-1;jj++)
{
if(fileCTime[jj]>fileCTime[jj+1])
{
t=fileCTime[jj];
fileCTime[jj]=fileCTime[jj+1];
fileCTime[jj+1]=t;
//char* ch=new char[strlen(srcFile1[jj])];
strncpy(ch,srcFile1[jj],strlen(srcFile1[jj]));
memset(srcFile1[jj],'\0',100);
strncpy(srcFile1[jj],srcFile1[jj+1],strlen(srcFile1[jj+1]));
memset(srcFile1[jj+1],'\0',100);
strncpy(srcFile1[jj+1],ch,strlen(ch));
memset(ch,'\0',100);
//delete [] ch;
}
}
}
string server="storescu 192.168.1.114 104 ";
string port=" -aec ACME_STORE -aet UIHCT -d --required --propose-lossless";
string file="CT_5d083a70a0da190b.dcm";
string strr;
const char* comm=nullptr;
for(int ii=0;ii<i;ii++)
{
usleep(1000000);
printf("the time is %d\n",fileCTime[ii]);
printf("the path is %s\n", srcFile1[ii]);
printf("the ii is %d\n", ii);
strr=server+srcFile1[ii]+port;
comm=strr.data();
system(comm);
}
return 0;
}