linux学习笔记——将当前时间信息写入文件

我看的书是《嵌入式linux——从入门到精通》陆桂来编著的这本书,本次做的小项目是将当前的信心写入文件当中,把书中的例子敲到电脑里面运行,出现各种问题,不知道是linux版本的问题还是gcc的问题还是库文件的问题。我加了几个头文件,另外改了一下printf那个函数,原来的printf函数为:

printf("%s",&writebuf);

我觉得要输出字符串,不能加取地址符号吧,即使输出地址,%s肯定也不对,这句肯定有问题,就将&符号删除了。以下是改好的程序,可以运行。

#include<fcntl.h>
#include<stdio.h>
#include<string.h>
#include<sys/time.h>
#include<time.h>
#include<unistd.h>
#include<sys/types.h>
int main(int argc,char *argv[])
{
    int temp,seektemp;
    int fd;
    char writebuf[50];
    struct timeval timenow,timeold;
    struct timezone timez;
    time_t timetemp;
    int j=0;
    int writeCounter = 0;
    gettimeofday(&timeold,&timez);
    if(argc!=2)
    {
        printf("Plz input the correct file name as'./exam39lseekFun filename string'!\n");
        return 1;
    }

    fd = open(*(argv+1),O_RDWR|O_CREAT,S_IRWXU);
    while(1)
    {
        while(1)
        {
            gettimeofday(&timenow,&timez);
            if((timenow.tv_sec-timeold.tv_sec) == 1)
            {
            timeold = timenow;
            break;
            }
        }
    time(&timetemp);
    sprintf(writebuf,"%s",ctime(&timetemp));


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值