[Linux文件]每隔1分钟创建一个文件,并且每隔1秒将当前时间信息写入到文件

//这是一个在参数指定文件中连续写入当前时间的应用
//每隔1分钟在当前目录下建立一个新文件,通过tm_sec是否为0来判断
//文件以1秒为时间间隔,将当前的时间写入文件,然后回车换行
#include 
#include 
#include 
#include 
#include
int main(int argc,char *argv[])
{

    time_t filetime;//时间结构体对象
    struct tm *p;//时间结构体指针
    int temp,seektemp;                         //偏移量计算中间量
    int fd;                                    //文件描述符
    char writebuf[50];                        //写字符串缓冲区
    char filenamebuf[10] = "File";             //文件名称缓冲区
    char timebuf[6];                          //时间缓冲区
    struct timeval timenow,timeold;            //时间变量
    struct timezone timez;
    int j = 0;
    int writeCounter = 0;                      //写入计数器

    if(argc!= 1)                               //如果参数错误
    {
        printf("Plz input the corrcet file name as './exam39lseekFun filename string'!\n");
        return 1;					//如果参数不正确则退出
    }

    gettimeofday(&timeold,&timez);             //取得一个时间信息作为参考时间信息
    time(&filetime);//获得时间参数
    p = localtime(&filetime);//获得时分秒参数,以供创建新文件
    sprintf(timebuf,"%02d%02d%02d",p->tm_hour,p->tm_min,p->tm_sec);
    strcat(filenamebuf,timebuf);//创建文件名
    strcat(filenamebuf,".txt");
    fd=open(filenamebuf,O_RDWR|O_CREAT,S_IRWXU);//创建文件

    // fd = open(*(argv+1),O_RDWR|O_CREAT,S_IRWXU); //打开文件如果没有则创建
    while(1)   //进入主循环
    {
        while(1)   //1毫秒延时判断
        {
            gettimeofday(&timenow,&timez);    //获取当前时间参数
            time(&filetime);//获得时间参数
            p = localtime(&filetime);//获得时分秒参数,以供创建新文件
            sprintf(timebuf,"%02d%02d%02d",p->tm_hour,p->tm_min,p->tm_sec);
            //printf("%d:%d:%d\n",p->tm_hour,p->tm_min,p->tm_sec);
            //时分秒信息放入timebuf缓冲区备用
            //gettimeofday(&timenow,&timez);    //获取当前时间参数
            if((timenow.tv_sec - timeold.tv_sec) == 1)  //如果到达一秒
            {
                timeold = timenow;    //更新保存的时间信息
                printf("%d:%d:%d\n",p->tm_hour,p->tm_min,p->tm_sec);
                //printf("p->tm_sec:%02d\n",p->tm_sec);
                break;                //1秒时间到,退出
            }
        }

        if(p->tm_sec==0)//如果是0秒,到1分钟了
        {
            char filenamebuf[]= "File";
            strcat(filenamebuf,timebuf);//创建文件名
            strcat(filenamebuf,".txt");
            fd=open(filenamebuf,O_RDWR|O_CREAT,S_IRWXU);//创建文件
            writeCounter=0;
            printf("new filename %s\n",filenamebuf);
        }
        time(&filetime);         //获得当前时间参数
        sprintf(writebuf,"%s",ctime(&filetime));     //将当前时间参数放入写缓冲区
        printf("%s",&writebuf);            //在屏幕上打印单签writebuf的内容
        if(writeCounter == 0)		//第一次写入
        {
            temp = write(fd,writebuf,strlen(writebuf));  //写入数据
            seektemp = lseek(fd,0,SEEK_CUR);   //获得当前的偏移量
            writeCounter++;   //写入计数器++
        }
        else
        {
            j = strlen(writebuf) * writeCounter;  //获得偏移量
            seektemp = lseek(fd,j,SEEK_SET);
            temp = write(fd,writebuf,strlen(writebuf));
            writeCounter++;
        }
    }
    close(fd);
    return 0;
}

已上机测试

成都创新互联于2013年创立,是专业互联网技术服务公司,拥有项目网站建设、成都网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元舟曲做网站,已为上家服务,为舟曲各地企业和个人服务,联系电话:18982081108


新闻名称:[Linux文件]每隔1分钟创建一个文件,并且每隔1秒将当前时间信息写入到文件
链接分享:http://lszwz.com/article/ppcdje.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款