1.fseek函数
第一个参数指向被搜索文件的FILE指针
第二个参数是从起始点的偏移量,long类型必须是,为正向后面偏移,为负向前面偏移
第三个参数标识起始点
如果正常返回0,否则返回-1
2.ftell函数
返回long类型标识,返回文件开始处到当前位置的字节数,可以用于循环使用
- #include<stdio.h>
- #include<stdlib.h>
- #define SIZE 40
- #define CNTL_Z '\032'
- int main(void){
- char name[SIZE];//记录打开的文件名
- FILE *fp;//文件指针
- long count = 0;//记录当前位置到文件起点的距离
- int ch;//记录文件中的一个字符
- long i;//控制for循环
- printf("Enter the name of the file to be processed:\n");//获得输入
- gets(name);
- if((fp = fopen(name,"rb")) == NULL){//获得文件指针
- printf("reverse can't open %s\n",name);
- exit(-1);
- }
- fseek(fp,0L,SEEK_END);//定位到最后
- count = ftell(fp);//获得最后位置到起始位置的距离
- for(i=1L;i<=count;i++){
- fseek(fp,-i,SEEK_END);//从最后位置开始一格格前进,可是为什么我使用fseek(fp,-1L,SEEK_CUR)不行呢?
- ch = getc(fp);//获取一个字符
- if(ch != CNTL_Z && ch != '\r'){//不打印二进制DOS下的回车符和文件结束
- putchar(ch);//输出一个字符
- }
- }
- putchar('\n');
- fclose(fp);
- getchar();
- return 0;
- }
转载于:https://blog.51cto.com/fanaticssk/829349