正在加载图片...
你现在所main fseek函数int #include stdio. h 函数/ FII main( if((i FILE"tp °返值: char file 功能: ename 801 for long length fel数 gets(filename) 函数原 fp=fopen(filename, rb"); 功能: if(fp==NULL) 表示) printf("file not found! n") 返值:1}8 fcl i fseek(fp, OL, SEEK END) length=ftell(fp) 例磁盘文什门 printf(" "Length of File is% d bytes" length) fclose(fp) 例求文件长度(ch12H01c) }stud3」;你现在所处的位置在:首页→C语言教学→幻灯片版→第十章 文件 17 ⚫ 函数原型: int fseek(FILE *fp,long offset,int whence) ⚫ 功能:改变文件位置指针的位置 ⚫ 返值:成功,返回0;失败,返回非0值 位移量(以起始点为基点 文件指针 ,移动的字节数) >0 向后移动 <0 向前移动 起始点 文件开始 SEEK_SET 0 文件当前位置 SEEK_CUR 1 文件末尾 SEEK_END 2 例 fseek(fp,100L,0); fseek(fp,50L,1); fseek(fp,-10L,2); ftell函数 函数原型: long ftell(FILE *fp) 功能:返回位置指针当前位置(用相对文件开头的位移量 表示) 返值:成功,返回当前位置指针位置;失败,返回-1L, 例 磁盘文件上有3个学生数据,要求读入第1,3学生数据并显示 main() { int i; FILE *fp; if((fp=fopen("studat","rb"))==NULL) { printf("can't open file\n");exit(0); } for(i=0;i<3;i+=2) { fseek(fp,i*sizeof(struct student_type),0); fread(&stud[i],sizeof(struct student_type),1,fp); printf("%s %d %d %s\n", stud[i].name,stud[i].num,stud[i].age,stud[i].addr); } fclose(fp); } #include <stdio.h> struct student_type { int num; char name[10]; int age; char addr[15]; }stud[3]; #include"stdio.h" main() { FILE *fp; char filename[80]; long length; gets(filename); fp=fopen(filename,"rb"); if(fp==NULL) printf("file not found!\n"); else { fseek(fp,0L,SEEK_END); length=ftell(fp); printf("Length of File is %1d bytes\n",length); fclose(fp); } } 例 求文件长度(ch12_101.c) fseek函数
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有