Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1633566
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: LINUX

2009-03-27 08:46:12

学会使用fsetpos fgetpos fscanf fprintf等函数的使用

 

 

#include <stdio.h>
#include <string.h>
#include <utmp.h>
#include <stdlib.h>

int main(void)
{
 FILE *fp,*fp1,*fp2;
 struct utmp utmp_buf;
 fpos_t pos;
 int num=0,cur_num=0;
 if( (fp=fopen( "/var/run/utmp","r"))==NULL )
 {
  perror("error when open the file ! \n");
  exit(1);
 }
 if( (fp1=fopen( "/tmp/utmp_info","w"))==NULL )
 {
  perror("error when open the file ! \n");
  exit(1);
 }
 
 while( (fread(&utmp_buf,sizeof(struct utmp),1,fp))>0 )
 {
  num++;
  printf("current_use: %s \n",utmp_buf.ut_user);
  fprintf(fp1,"current_use: %s \n",utmp_buf.ut_user);
 }
 
 printf("now, read for the last 4 line \n");
 fseek(fp,-4*sizeof(struct utmp),SEEK_END);
 while( (fread(&utmp_buf,sizeof(struct utmp),1,fp))>0 )
 {
  printf("current_use: %s \n",utmp_buf.ut_user);
 }

 printf("now,use fsetpos\fgetpos to read last 4 line \n");
 rewind(fp);
 while( (fread(&utmp_buf,sizeof(struct utmp),1,fp))>0 )
 {
  cur_num++;
  if( cur_num==(num-4) )
  {
   fgetpos(fp,&pos);
   break;
  }
 }
    fsetpos(fp,&pos);
 while( (fread(&utmp_buf,sizeof(struct utmp),1,fp))>0 )
 {
  printf("current_use: %s \n",utmp_buf.ut_user);
 }

 fclose(fp);
 
}

阅读(538) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~