遍历文件夹下文件 打印创建时间 并按创建时间排序 然后按时间每隔一秒发送给pacs服务器(自动化模拟CT部分代码)

直接上代码:

#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;
 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值