程序要求:
(1)读写一个test.txt文件,每隔1秒往文件中写入一行时间日期数据;
1、 2012-8-7 1:2:3
....
(2)下次启动程序时能够追加到原文件之后,并且序号能够衔接上原先序号;
程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(int argc, const char *argv[])
{
FILE *file;
struct tm *t1;
time_t t;
char buf[100];
int line = 1;
int c;
memset(buf, 0, sizeof(buf));
if ((file = fopen("test.txt", "a+")) < 0)
{
perror("failed to open test.txt");
exit(-1);
}
while ((c = getc(file)) != EOF) //计算行数,用于下次打开时能够衔接