2.文件IO---time(打印当地时间)
来源:互联网 发布:arm 模拟器 windows 编辑:程序博客网 时间:2024/05/23 10:09
《Makefile》
CC=gcc
TEXT=time
SVC=file_time.c debug.h
$(TEXT): $(SVC)
$(CC) -o $@ $^
clean:
rm -rf $(TEXT) *.o
*************************
《头文件》
/*Comment/uncomment the following line to disable/enable debugging,
OR define(or NOT) it in Makefile.
*/
//#define NDEBUG
#undef pr_debug /* undef it, just in case */
#ifndef NDEBUG
#ifdef __KERNEL__
/* This one if debugging is on, and kernel space */
#define pr_debug(fmt, args...) printk( KERN_ERR fmt, ## args)
#else
/* This one for user space */
#define pr_debug(fmt, args...) fprintf(stderr, fmt, ## args)
#endif
#else
#define pr_debug(fmt, args...) /* not debugging: nothing */
#endif
*************************
/* usage example: ./file_time test.txt */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <time.h>
#include "debug.h"
void usage (const char *s)
{
printf("usage:\n\t%s time_file\n", s);
}
int main(int argc, char **argv)
{
FILE *fp = NULL;
time_t seconds;
struct tm *pTime = NULL;
char buf[BUFSIZ];
int i = 0;
if(argc != 2) {
usage(argv[0]);
exit(1);
}
fp = fopen(argv[1], "a+");
assert(fp != NULL);
if (!fp ) {
usage(argv[0]);
exit(1);
}
fseek(fp, 0, SEEK_SET); //rewind(fp);
while(!feof(fp)) {
fgets(buf, BUFSIZ,fp);
if (buf[0] != EOF)
++i;
}
pr_debug("i =%d \n", i);
while(1) {
seconds = time (NULL);
pTime = localtime(&seconds);
assert(pTime);
sprintf(buf, "%d. %d-%d-%d %d:%d:%d\n", i++, pTime->tm_year+1900, pTime->tm_mon, pTime->tm_mday,
pTime->tm_hour, pTime->tm_min, pTime->tm_sec);
pr_debug("%s\n", buf);
fwrite(buf, strlen(buf), 1, fp);
fflush(fp);
sleep(1);
}
fclose(fp);
return 0;
}
***************
执行方式:./ time file_time
结果:
i =1
1. 2011-11-7 17:50:34
2. 2011-11-7 17:50:35
3. 2011-11-7 17:50:36
4. 2011-11-7 17:50:37
5. 2011-11-7 17:50:38
6. 2011-11-7 17:50:39
7. 2011-11-7 17:50:40
8. 2011-11-7 17:50:41
9. 2011-11-7 17:50:42
10. 2011-11-7 17:50:43
11. 2011-11-7 17:50:44
12. 2011-11-7 17:50:45
13. 2011-11-7 17:50:46
14. 2011-11-7 17:50:47
15. 2011-11-7 17:50:48
16. 2011-11-7 17:50:49
17. 2011-11-7 17:50:50
- 2.文件IO---time(打印当地时间)
- Python每隔一秒钟打印当地时间
- 如何利用C语言来获取当地时间和UTC时间? (设当地时间是北京时间)
- 黑马程序员--IO流(Properties、打印流 、文件切割与合并)
- (转载) Java IO 之 FileReader 读取文件打印在控制台(处理汉字乱码bug)
- 从GMT时间转换到当地时间(北京时间)
- java使用IO打印流输出到文件
- IO学习(十五)PrintStream打印流
- 史上最全IO框架六(打印流)
- 46-IO流(Properties集合的-练习)12 47-IO流(综合练习-文件清单列表. 48-IO流(打印流-PrintStream) 49-IO流(打印流-PrintWriter).
- 文件读写(IO)
- 复制文件(IO)
- IO流 FileInputStream(字节流) FileReader(字符流能打印出中文)把文件中内容读出来
- 文件IO(文件空洞)
- 文件IO(节点文件)
- 文件IO(文件指针)
- 文件IO(标准IO缓冲区)
- IO操作文件内容打印到控制台,将文件内容复制到另外一个文件
- ubuntu 11.10 配置nfs
- checking for tgetent()... configure: error: NOT FOUND!
- 嵌入式数据库--SQLite简单应用(三)--基于Qt3+SQLite3的通信录
- All of Django'Models
- 【转载】开发经验总结,很受用。
- 2.文件IO---time(打印当地时间)
- 隐婚
- jquery 获取窗体宽度,可视化宽度
- ubuntu11.10安装samba共享文件
- UEFI Images 转载
- 哈佛商业评论:社交网络SNS的未来前景
- UEFI Drivers & UEFI Driver Model
- Linux系统信息查看命令大全
- NoSQL 数据库简介