收藏 分享(赏)

C语言编程学习课件 (62).pdf

上传人:职教中国 文档编号:13894354 上传时间:2022-11-06 格式:PDF 页数:15 大小:899.02KB
下载 相关 举报
C语言编程学习课件 (62).pdf_第1页
第1页 / 共15页
C语言编程学习课件 (62).pdf_第2页
第2页 / 共15页
C语言编程学习课件 (62).pdf_第3页
第3页 / 共15页
亲,该文档总共15页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、Programming In C there is a position pointer in a file, which points to the file header when the file is opened.When using file read/write functions, the position pointer is moved backward to the next read/write position.That is, file read/write functions introduced earlier can only read/write files

2、 in order. In order to read/write files randomly, the file positioning is used functions described in this tutorial to force the position to change by the position pointer. The functions mainly include: fseek( ), ftell( )和 rewind( )。 Position in a File 1. fseek( ) move the position of the position p

3、ointer inside a file. The general calling form: fseek(file pointer, offsets, starting position); Explain: ( 1) “ file pointer” points to the file to be moved。 ( 2) “ offsets” is the number of bytes to be moved by a long integer, namely, the number of offset bytes from the starting point. Positive va

4、lues mean moving backward and negative values mean moving forward. Position in a File 1. fseek( ) starting position symbolic numbers header SEEK_SET 0 current SEEK_CUR 1 tail SEEK_END 2 Explain: ( 3) “ starting position” indicates where to start moving, which can be set at three different positions,

5、 defined by the system as the following three symbolic constants and represented by corresponding numbers. Position in a File 1. fseek( ) e.g.: ( 1) Move the position pointer to the position of 100 bytes away from the file header: fseek(fp, 100L, 0); ( 2) Move the position pointer to the position of

6、 5 bytes away from the file tail: fseek(fp, -5L, SEEK_END); ( 3) Move backward from the current position to the next structure data: fseek(fp, sizeof(struct student), 1); Position in a File 2. ftell( ) Obtain the current position of the position pointer inside a file.The calling form: ftell(file poi

7、nter); Explain: ( 1) “ file pointer” Points to the file want to be moved ( 2) When it is executed normally, the return value of the function is the displacement relative to the file header and is a long integer. If there is an error, the return value is -1L. e.g. n=ftell(fp); Position in a File 3. r

8、ewind( ) Reset the position pointer to the head of the file.The calling form: rewind(file pointer); Explain: “ file pointer” Points to the file want to be moved。 e.g rewind(fp); The function is called, all subsequent read-write operations start from the file header. Position in a File Example 6 Ente

9、r the name of a file from the keyboard, find out the length of the file, and then display the contents of the file. Problem Analysis: ( 1) When the file is opened,the internal position pointer is in the file header.The fseek() is used to move the position pointer to the file tail first, and then fte

10、ll() is used to find the current position. This result is the length of the file. ( 2) After finding the length of the file, the position pointer can be reset by calling the rewind(), and then read out the contents of the file in sequence by calling the fgetc(). Position in a File Program Code: #inc

11、lude “ stdlib.h” #include “ stdio.h” void main( ) FILE *fp; char filename50, ch; long length; printf(“Please input the filename : ”); scanf(“% s”,filename ); if(fp=fopen(filename, “ rb” )=NULL) printf(“Cant open the file ! n”); exit(0); fseek(fp, 0L, 2); length=ftell(fp); printf(“File length : %ldn”

12、,length ); rewind(fp); Position in a File Program Code: while(!feof(fp)) ch=fgetc(fp); putchar(ch); fclose(fp); Running Results: Please input the filename: test.dat File length: 10 Abcde12345 Position in a File Example 7 There are ten students data stored on the disk file “ stud.dat” , and read out

13、the data of the 1st, 3rd, 5th, 7th and 9th students and display them on the screen. Problem Analysis: Open the file and use the opening method“ rb; In the for loop, the fseek() is used to locate the position pointer to the data to be displayed;Read data by the free(), and display data by the printf(

14、). Position in a File Program Code: #include “ stdlib.h” #include “ stdio.h” struct student char name20; int num; int age; char sex; stud10; void main( ) int i; FILE *fp; if(fp=fopen(“stud.dat”, “ rb” )=NULL) printf(“Cant open the file ! n”); exit(0); Position in a File Program Code: for(i=0;i10;i+=

15、2) fseek(fp, i*sizeof(struct student), 0); fread(&studi,sizeof(struct student),1,fp); printf(“%s %d”,studi.name,studi.num); printf(“ %d %c n”,studi.age,studi.sex); fclose(fp); Running Results: Zhangs 1001 18 m Lius 1003 20 f Zhaom 1005 19 m Qians 1007 20 m WangXiao 1009 20 f Position in a File The file positioning functions: fseek(), ftell(), and rewind(), which can move the file position pointer to any position in a file. Position in a File Programming In C

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 大学课件

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报