Linux system programming 31 System data files and information-time experiment print time log

Experiment 1 Print time 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);
	
}

In this way of writing, there will be two situations.
Situation 1: After the program has been running for a period of time, ctrl + c will interrupt the program, check the file and find that the /tmp/out file is empty.
Situation 2: Wait for a long time, and wait until the /tmp/out file is full. Check the file and find that there is content in the /tmp/out file

The reason for this is: Except for our standard terminal devices, other stream formats are fully buffered by default, so the newline'\n' in our fprintf can no longer serve as a refreshing buffer, because of this When writing a file, the file is in full buffer mode, as long as there is no buffer that is not full, the buffer will not be refreshed. The content will not be written to buf, we need to refresh the stream.

#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$ 

Incorrect delivery time

#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

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/106041033
Recommended