您的位置:首页 > 运维架构 > Linux

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

2016-04-20 10:13 465 查看
我看的书是《嵌入式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));
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  嵌入式 linux