以日期时间为文件名的字符串的定量增长

以日期时间为文件名的字符串的定量增长。经常应用于多个文件的读取file1,file2,file3.本代码中的文件名仅限于 “XXXXXXXX_XXXX.txt”类型。

/* *Copyright (c) 2015 xiu *All rights reserved. * *FileName : *摘要:将以日期时间为文件名的字符串定量增长。 *如文件名为“20080808_1755.txt”经过15分钟后时间为“20080808_1810.txt” */

#include <iostream>
#include <string>
using namespace std;

//将string类型的字符串转换成char *型的
void convert(char *name,string t,int char_num)
{
    for (int i=0;i<char_num;i++)
    {
        name[i]=t[i];
    }
    name[i]='\0';
}


//计算间隔为delay的下一个文件名
void computeNextFileName(char *filename,int delay,char * minute,char * hour,char * day,char *month,char * year)
{
    //整型的时间
    int t_minute,t_hour,t_day,t_month,t_year;

    string name=(string)filename; 

    //获取整型的年
    string str=name.substr(0,4);
    convert(year,str,4);
    t_year=atoi(year);

    //获取整型的月
    str=name.substr(4,2);
    convert(month,str,2);
    t_month=atoi(month);

    //获取整型的日
    str=name.substr(6,2);
    convert(day,str,2);
    t_day=atoi(day);

    //获取整型的小时
    str=name.substr(9,2);
    convert(hour,str,2);
    t_hour=atoi(hour);

    //获取整型的分钟
    str=name.substr(11,2);
    convert(minute,str,2);
    t_minute=atoi(minute);

    //分钟数增加delay
    t_minute+=delay;

    //判断分钟数是否大于60
    while (t_minute>=60)
    {
        t_minute-=60;
        t_hour+=1;
    }

    //判断小时数是否大于24
    while (t_hour>=24)
    {
        t_hour-=24;
        t_day+=1;
    }

    //判断天数
    switch(t_month)
    {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if (t_day>31)
        {
            t_day-=31;
            t_month+=1;
            break;
        }
    case 4:
    case 6:
    case 9:
    case 11:
        if (t_day>30)
        {
            t_day-=30;
            t_month+=1;
            break;
        }
    case 2:
            if (t_year%4==0)
            {
                if (t_day>28)
                {
                    t_day-=28;
                    t_month+=1; 
                }
                break;
            }
            else 
            {
                if (t_day>29)
                {
                    t_day-=29;
                    t_month+=1; 
                }
                break;
            }
        }
        if (t_month>12)
        {
            t_year+=1;
        }


    // cout<<t_year<<"\t"<<t_month<<"\t"<<t_day<<"\t"<<t_hour<<"\t"<<t_minute<<endl;

        //将整型数转换成字符串
        itoa(t_year,year,10);
        itoa(t_month,month,10);
        itoa(t_day,day,10);
        itoa(t_hour,hour,10);
        itoa(t_minute,minute,10);

        if (t_month<10)
        {
            month[1]=month[0];
            month[0]='0';
        }
        if (t_day<10)
        {
            day[1]=day[0];
            day[0]='0';
        }
        if (t_hour<10)
        {
            hour[1]=hour[0];
            hour[0]='0';
        }
        if (t_minute<10)
        {
            minute[1]=minute[0];
            minute[0]='0';
        }

}


void main()
{
    char filename[20] = "20120606_2155.txt";//需要转换的文件名

    int delay = 15; //定量增长的时间 (分钟)

    int count = 10; //增长的次数



    char minute[3],hour[3],day[3],month[3],year[5]; //获取增长后的时间


    //xiu——————test

    //cout<<"filename = "<<filename<<endl;
    //cout<<year<<"\t"<<month<<"\t"<<day<<"\t"<<hour<<"\t"<<minute<<endl;
    //cout<<t_year<<"\t"<<t_month<<"\t"<<t_day<<"\t"<<t_hour<<"\t"<<t_minute<<endl;

    for (int i = 0;i< count;i++)
    {
        char result_name[20]="";


        //计算下一个文件名
        computeNextFileName(filename,delay*i,minute,hour,day,month,year);

        //xiu-----------test

        // cout<<year<<"\t"<<month<<"\t"<<day<<"\t"<<hour<<"\t"<<minute<<endl;

        //将结果存放于result_name中

        strcat(result_name,year);
        strcat(result_name,month);
        strcat(result_name,day);
        strcat(result_name,"_");
        strcat(result_name,hour);
        strcat(result_name,minute);
        strcat(result_name,".txt");


        cout<<result_name<<endl;

    }       



}

你可能感兴趣的:(c,文件名,增长,定量)