当前位置:网站首页>C - file operations fseek () function, ftell, rewind, rounding

C - file operations fseek () function, ftell, rewind, rounding

2022-08-11 05:45:00 Yunyi 943

前言

        前面,I talked about the second step of file manipulation:文件的读写操作,It introduces the usage of various read and write functions in detail,有兴趣的小伙伴可以去看看:

C语言——文件操作(2)文件的读写操作_

        接下来,Let me talk about the usage of these three functions in the title.

目录

前言

一.fseek函数——

     1. 功能:

2.参数解析:

 3.练习1.要求输出字符c

    练习2.要求输出字符f

        A.方法1.采用SEEK_CUR The position the current pointer points to is the center .

        B.方法2:SEEK_CUR    The current position of the file pointer is centered.

        C.方法3:SEEK_END    The end of the file is centered.

二.ftell函数

1.功能:

三.rewind函数

1.功能:

2.代码实践:


一.fseek函数——

     1. 功能:

根据文件指针的位置和偏移量来定位文件指针.(只用来:定位!!!)

2.参数解析:

fseekThe first parameter is the stream,第二个参数为偏移量,The third parameter is the position where the file pointer is located
                        /*SEEK_SET    Centered at the beginning of the file
                        SEEK_CUR    The current position of the file pointer is centered
                        SEEK_END    The end of the file is centered*/

The picture below is not usedfseek的代码:

int main() {
	FILE* pf = fopen("file.txt", "r");
	if (pf == NULL) {
		printf("%s\n", strerror(errno));
		return 1;
	}

	char ch = 0;
	ch=fgetc(pf);
	printf("%c\n", ch);

	ch = fgetc(pf);
	printf("%c\n", ch);

	ch = fgetc(pf);
	printf("%c\n", ch);

	fclose(pf);
	pf = NULL;
	return 0;
}

        代码讲解:字符输出函数——fgetc(),一次读取一个字符.使用fgetc函数时,The first time the pointer can only point to the beginning,每使用一次fgetcto read a character,The pointer will step backwards,This method is rigid,You can't point to whatever file content you want to point to.

 

        上图为file.txt文件的内容 

 

        于是有了fseek函数,You can arbitrarily let the file pointer point to the desired location,to get the desired file content.

 3.练习1.要求输出字符c

int main() {
	FILE* pf = fopen("file.txt", "r");
	if (pf == NULL) {
		printf("%s\n", strerror(errno));
		return 1;
	}
	char ch = 0;
	//	//文件内容:abcdef


	fseek(pf, 2, SEEK_SET);
	
	ch = fgetc(pf);//chReceives the content pointed to by the file pointer,c字符
	printf("%c\n", ch);

    fclose(pf);
    pf=NULL;
    
    return 0;
    }

代码讲解: fseek(pf, 2, SEEK_SET);

                This code means:Point the file pointer to the beginning of the file contents,偏移量为2,偏移量0代表a,2代表0+2,That is, the file pointer points toa之后的第2个字符c.

练习2.要求输出字符f


int main() {
	FILE* pf = fopen("file.txt", "r");
	if (pf == NULL) {
		printf("%s\n", strerror(errno));
		return 1;
	}
	char ch = 0;

	//方法1.
	fseek(pf, 2, SEEK_CUR);
	ch = fgetc(pf);		//chReceives the content pointed to by the file pointer,f字符
	printf("方法1:%c\n", ch);


	//方法2:
	fseek(pf, 5, SEEK_SET);
	ch = fgetc(pf);			//chReceives the content pointed to by the file pointer,f字符
	printf("方法2:%c\n", ch);


	//方法3:
	fseek(pf, -1, SEEK_END);
	ch = fgetc(pf);			//chReceives the content pointed to by the file pointer,f字符
	printf("方法3:%c\n", ch);

	fclose(pf);
	pf = NULL;
	return 0;
}

代码讲解:The way this code opens the file is:读文件

        A.方法1.采用SEEK_CUR The position the current pointer points to is the center .

        fseek(pf, 2, SEEK_CUR);//This code means:Point the file pointer to the character position pointed to by the current pointer,因为在练习1中,After the file pointer has been read,The pointer automatically jumps to the next character positiond,当前偏移量0代表d, Offset is set to 2,2代表字符f.

        B.方法2:SEEK_CUR    The current position of the file pointer is centered.

        fseek(pf, 5, SEEK_SET);//This code means:Point the file pointer to the beginning of the file,当前偏移量0代表a, Offset is set to 5,5代表字符f.

        C.方法3:SEEK_END    The end of the file is centered.

        fseek(pf, -1, SEEK_END);//意为:Point the file pointer to the end of the file content,文件内容为abcdef,character at the end positionf之后的位置,偏移量-1,Move the pointer forward one position,Enables pointers to point to charactersf.


二.ftell函数

long int ftell ( FILE * stream );

1.功能:

         返回文件指针相对于起始位置的偏移量.

2.代码实践:

int main() {
	FILE* pf = fopen("file2.txt", "r");
	if (pf == NULL) {
		printf("%s\n", strerror(errno));
		return 1;
	}
	char ch = 0;
		//文件内容:abcdef

	fseek(pf, 2, SEEK_SET);
	ch = fgetc(pf);//chReceives the content pointed to by the file pointer,c字符
	printf("%c\n", ch);
	printf("%d\n",ftell(pf));//3


	fseek(pf, -1, SEEK_END);
	ch = fgetc(pf);//chReceives the content pointed to by the file pointer,f字符
	printf("%c\n", ch);
	printf("%d\n", ftell(pf));//



	fclose(pf);
	pf = NULL;
	return 0;
}

 

调试结果如下:

 

代码讲解:This file opens with :读文件 

                结果为3的原因:因为ch接收了pfcharacter at the location pointed toc,输出后,The file pointer automatically jumps to the next characterd的位置,ftellThe function is to calculate the offset distance of the current position of the pointer relative to the beginning of the file.

                结果为6的原因:chThe character pointed to by the file pointer was receivedf后,Auto backward,points to the end position,ftellThe function calculates the offset distance of the end position relative to the beginning of the file6,The offset distance is 0Represents the first file contenta;The offset distance is 5,Indicates the first in the file content6content charactersf.


三.rewind函数

 

1.功能:

让文件指针的位置回到文件的起始位置.

2.代码实践:

int main() {
	FILE* pf = fopen("file2.txt", "r");
	if (pf == NULL) {
		printf("%s\n", strerror(errno));
		return 1;
	}
	char ch = 0;
	//	//文件内容:abcdef

	//练习1.要求输出字符c
	fseek(pf, 2, SEEK_SET);
	ch = fgetc(pf);//chReceives the content pointed to by the file pointer,c字符
	printf("%c\n", ch);
	printf("%d\n", ftell(pf));//偏移量为3

	rewind(pf);//让文件指针回到文件起始位置
	printf("%d\n", ftell(pf));
	
	printf("----------------\n");

	fseek(pf, -1, SEEK_END);
	ch = fgetc(pf);//chReceives the content pointed to by the file pointer,f字符
	printf("%c\n", ch);
	printf("%d\n", ftell(pf));//6

	rewind(pf);//让文件指针回到文件起始位置
	printf("%d\n", ftell(pf));//0
	

	fclose(pf);
	pf = NULL;
	return 0;
}

 

 

好了,The functions and usage of these three functions are discussed here,If you find it useful, remember to click on the triple~,下期见!

原网站

版权声明
本文为[Yunyi 943]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110512554702.html