11.6标准IO

11.6标准IO_第1张图片

#include 
//计算行号
int len(FILE *fp)
{
	char s[10]="";
	int n=0;
	while(fgets(s,sizeof(s),fp)!=NULL)
	{
		if(s[strlen(s)-1]=='\n')
		{
			n++;
		}
	}
	return n;
}
int main(int argc, const char *argv[])
{
	//创建文件指针
	FILE *fp=NULL;
	if((fp=fopen(argv[1],"a+"))==NULL)
	{
		perror("fopen");
		return -1;
	}
	fseek(fp,0,SEEK_SET);
	int n=len(fp);
	printf("%d",n);
	fseek(fp,0,SEEK_END);
	while(1)
	{
		time_t new_t,old_t;
                time(&new_t);
                struct tm*tm=localtime(&new_t);
		if(old_t!=new_t)
		{
		old_t=new_t;
                char temp[128]="";
                snprintf(temp,sizeof(temp),"%2d.%02d:%02d:%02d\n",++n,\
				tm->tm_hour,\
                                tm->tm_min,\
                                tm->tm_sec);
                        fwrite(temp,1,sizeof(temp),fp);
			fflush(fp);
                }	
	}
	fclose(fp);
	return 0;
}

11.6标准IO_第2张图片​​​​​​​
​​​​​​11.6标准IO_第3张图片

你可能感兴趣的:(linux)