2008年6月25日 星期三

tracker_write

為免 i/o buffer 令一些 tracking message 印唔切, 寫了個用 fflush 清 stdout buffer o既 program 比 script 呼叫.


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

int main(int argc, char *argv[])
{
FILE *fp;
int pres;

time_t now;
struct tm *stime;
char timeval[20];

if (argc < 3)
{
fprintf(stderr,"%s \n",argv[0]);
exit(-1);
}

if (fp = fopen(argv[1],"a"))
{
now = time(NULL);
stime = localtime(&now);
sprintf(timeval,"%d%02d%02d %02d:%02d:%02d",stime->tm_year + 1900,stime->tm_mon + 1,stime->tm_mday,stime->tm_hour,stime->tm_min,stime->tm_sec);

pres = fprintf(fp,"%s %s\n",timeval,argv[2]);
fflush(fp);
fclose(fp);
if (pres < 0)
{
fprintf(stderr,"File %s write error.\n",argv[1]);
}
}
else
{
fprintf(stderr,"File %s could not open.\n",argv[1]);
exit(-1);
}
}

沒有留言: