Linux系统编程31 系统数据文件和信息 -时间实验 打印时间log

实验1 打印时间log

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define BUFSIZE 1024

#define FNAME "/tmp/out"

int main()
{
	FILE *fp;
	char buf[BUFSIZE];
	int count = 0;
	time_t stamp;
	struct tm *tm;

	fp = fopen(FNAME,"a+");
	if(fp == NULL)
	{
		perror("fopen()");
		exit(1);
	}
		

	while (fgets(buf,BUFSIZE,fp) != NULL)
		count ++;


	while(1)
	{
		time(&stamp);
		tm = localtime(&stamp);
		fprintf(fp,"%-4d%d-%d-%d %d:%d:%d\n",++count,\
			tm->tm_year,tm->tm_mon,tm->tm_mday,\
			tm->tm_hour,tm->tm_min,tm->tm_sec);
		
		//fflush(fp);
		sleep(1);
	}	

	fclose(fp);
	
}

这样的写法,会出现两种情况
情况1:程序运行一段时间后 ctrl + c 中断程序,查看文件 ,发现 /tmp/out 文件为空
情况2:等很久,等到 /tmp/out 文件写满了,查看文件,发现 /tmp/out 文件中有内容

发生这种情况的原因是 :除了我们的标准的终端设备,其他流形式默认是全缓冲形式,所以我们的 fprintf 中的换行 ‘\n’ 已经不能起到一个刷新缓冲区的作用了,因为此时写文件,文件是全缓冲模式,只要没有缓冲区没有写满,就不会刷新缓冲区。就不会将内容写入buf,我们需要刷新流。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define BUFSIZE 1024

#define FNAME "/tmp/out"

int main()
{
	FILE *fp;
	char buf[BUFSIZE];
	int count = 0;
	time_t stamp;
	struct tm *tm;

	fp = fopen(FNAME,"a+");
	if(fp == NULL)
	{
		perror("fopen()");
		exit(1);
	}
		

	while (fgets(buf,BUFSIZE,fp) != NULL)
		count ++;


	while(1)
	{
		time(&stamp);
		tm = localtime(&stamp);
		fprintf(fp,"%-4d%d-%d-%d %d:%d:%d\n",++count,\
			tm->tm_year,tm->tm_mon,tm->tm_mday,\
			tm->tm_hour,tm->tm_min,tm->tm_sec);
		
		fflush(fp);
		sleep(1);
	}	

	fclose(fp);
	
}




mhr@ubuntu:~/work/linux/muluheyonghucaozuo/30$ gcc timelog.c 
timelog.c: In function ‘main’:
timelog.c:34:14: warning: format ‘%d’ expects a matching ‘int’ argument [-Wformat=]
   fprintf(fp,"%-4d%d-%d-%d %d:%d:%d:%d\n",++count,\
              ^
timelog.c:39:3: warning: implicit declaration of function ‘sleep’ [-Wimplicit-function-declaration]
   sleep(1);
   ^
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/30$ 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/30$ 
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/30$ ./a.out 

mhr@ubuntu:~/work/linux/muluheyonghucaozuo/30$ tail -f /tmp/out
10  120-4-10 4:19:0
11  120-4-10 4:19:1
12  120-4-10 4:19:2
13  120-4-10 4:19:3
14  120-4-10 4:19:4
15  120-4-10 4:19:5
16  120-4-10 4:19:6
17  120-4-10 4:19:7
18  120-4-10 4:19:8
19  120-4-10 4:19:9
20  120-4-10 4:19:10
...
^C
mhr@ubuntu:~/work/linux/muluheyonghucaozuo/30$ 

发下时间不正确

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define BUFSIZE 1024

#define FNAME "/tmp/out"

int main()
{
	FILE *fp;
	char buf[BUFSIZE];
	int count = 0;
	time_t stamp;
	struct tm *tm;

	fp = fopen(FNAME,"a+");
	if(fp == NULL)
	{
		perror("fopen()");
		exit(1);
	}
		

	while (fgets(buf,BUFSIZE,fp) != NULL)
		count ++;


	while(1)
	{
		time(&stamp);
		tm = localtime(&stamp);
		fprintf(fp,"%-4d%d-%d-%d %d:%d:%d\n",++count,\
			tm->tm_year+1990,tm->tm_mon+1,tm->tm_mday,\
			tm->tm_hour,tm->tm_min,tm->tm_sec);
		
		fflush(fp);
		sleep(1);
	}	

	fclose(fp);
	
}


mhr@ubuntu:~/work/linux/muluheyonghucaozuo/30$ tail -f /tmp/out
38  2110-5-10 4:26:3
39  2110-5-10 4:26:4
40  2110-5-10 4:26:5
41  2110-5-10 4:26:6
42  2110-5-10 4:26:7
43  2110-5-10 4:26:8
44  2110-5-10 4:26:9
45  2110-5-10 4:26:10
46  2110-5-10 4:26:11
47  2110-5-10 4:26:12
48  2110-5-10 4:26:21
49  2110-5-10 4:26:22
50  2110-5-10 4:26:23
51  2110-5-10 4:26:24
52  2110-5-10 4:26:25
53  2110-5-10 4:26:26
54  2110-5-10 4:26:27
^C

猜你喜欢

转载自blog.csdn.net/LinuxArmbiggod/article/details/106041033