标准I/O

来源:互联网 发布:网络终端机服务器软件 编辑:程序博客网 时间:2024/05/28 05:15
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <strings.h>#include <time.h>int main(int argc, char *argv[]){if (argc < 2) {fprintf(stderr, "Usage: %s <filename>\n", argv[0]);return -1;}FILE *fp = NULL;if (NULL == (fp = fopen(argv[1], "a+"))) {perror("fopen");return -1;}int line = 0;char buf[100];while (NULL != fgets(buf, sizeof(buf), fp)) {if (buf[strlen(buf)-1] == '\n')line++;}time_t t;struct tm *tm;while (1) {time(&t);tm = localtime(&t);sprintf(buf, "%2d, %4d-%2d-%2d %2d:%2d:%2d\n", ++line, \tm->tm_year+1900, tm->tm_mon+1, \tm->tm_mday, tm->tm_hour, tm->tm_min, \tm->tm_sec);fputs(buf, fp);fflush(fp);sleep(1);}fclose(fp);return 0;}