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));
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 socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程
- Linux 下无损图片压缩小工具介绍