1.fseek函数

第一个参数指向被搜索文件的FILE指针

第二个参数是从起始点的偏移量,long类型必须是,为正向后面偏移,为负向前面偏移

第三个参数标识起始点

 

如果正常返回0,否则返回-1

2.ftell函数

返回long类型标识,返回文件开始处到当前位置的字节数,可以用于循环使用

 

 
  
  1. #include<stdio.h> 
  2. #include<stdlib.h> 
  3. #define SIZE 40 
  4. #define CNTL_Z '\032' 
  5.  
  6. int main(void){ 
  7.     char name[SIZE];//记录打开的文件名  
  8.     FILE *fp;//文件指针  
  9.     long count = 0;//记录当前位置到文件起点的距离  
  10.     int ch;//记录文件中的一个字符  
  11.     long i;//控制for循环  
  12.      
  13.      
  14.     printf("Enter the name of the file to be processed:\n");//获得输入  
  15.     gets(name); 
  16.     if((fp = fopen(name,"rb")) == NULL){//获得文件指针  
  17.         printf("reverse can't open %s\n",name); 
  18.         exit(-1); 
  19.     } 
  20.     fseek(fp,0L,SEEK_END);//定位到最后  
  21.     count = ftell(fp);//获得最后位置到起始位置的距离 
  22.     for(i=1L;i<=count;i++){ 
  23.         fseek(fp,-i,SEEK_END);//从最后位置开始一格格前进,可是为什么我使用fseek(fp,-1L,SEEK_CUR)不行呢?  
  24.         ch = getc(fp);//获取一个字符  
  25.         if(ch != CNTL_Z && ch != '\r'){//不打印二进制DOS下的回车符和文件结束  
  26.             putchar(ch);//输出一个字符  
  27.         } 
  28.     } 
  29.     putchar('\n'); 
  30.     fclose(fp); 
  31.     getchar(); 
  32.     return 0;