温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-04-01 10:55:11 来源:网络 阅读:1290 作者:银河星君 栏目:编程语言
//这是一个在参数指定文件中连续写入当前时间的应用
//每隔1分钟在当前目录下建立一个新文件,通过tm_sec是否为0来判断
//文件以1秒为时间间隔,将当前的时间写入文件,然后回车换行
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include<time.h>
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;
}

已上机测试

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI