C language - detailed explanation of file operation functions fseek, ftell and rewind

Preface

        Earlier, I talked about the second step of file operation: file read and write operations. It introduced the usage of various read and write functions in detail. Interested friends can check it out:

C language - file operations (2) file read and write operations_

        Next, let me talk about the usage of these three functions in the title.

Table of contents

Preface

1.fseek function——

     1. Function:

2. Parameter analysis:

 3. Exercise 1. Require the output character c

    Exercise 2. Ask to output the character f

        A. Method 1. Use the position pointed by the current SEEK_CUR pointer as the center.

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

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

2. ftell function

1. Function:

3.rewind function

1. Function:

2. Code practice:


1.fseek function——

     1.  Function:

Position the file pointer based on its position and offset. (Only used for: positioning!!!)

2. Parameter analysis:

The first parameter of fseek is the stream, the second parameter is the offset, and the third parameter is the position of the file pointer
                        /*SEEK_SET is 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 shows the code without fseek:

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;
}

        Code explanation: Character output function - fgetc(), reads one character at a time . When using the fgetc function, the pointer can only point to the beginning for the first time. Each time fgetc is used, one character can be read, and the pointer will gradually move backward. This method is very rigid, and you cannot point to whichever file content you want.

 

        The picture above shows the contents of the file.txt file. 

 

        So with the fseek function, you can make the file pointer point to the desired location as you like, so as to obtain the desired file content.

 3. Exercise 1. Require the output character 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);//ch接收文件指针指向的内容,c字符
	printf("%c\n", ch);

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

Code explanation:  fseek(pf, 2, SEEK_SET);

                This code means: point the file pointer to the beginning of the file content, the offset is 2, offset 0 represents a, 2 represents 0+2, that is, the file pointer points to the second character c after a.

Exercise 2. Ask to output the character 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);		//ch接收文件指针指向的内容,f字符
	printf("方法1:%c\n", ch);


	//方法2:
	fseek(pf, 5, SEEK_SET);
	ch = fgetc(pf);			//ch接收文件指针指向的内容,f字符
	printf("方法2:%c\n", ch);


	//方法3:
	fseek(pf, -1, SEEK_END);
	ch = fgetc(pf);			//ch接收文件指针指向的内容,f字符
	printf("方法3:%c\n", ch);

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

Code explanation: This code opens the file by: reading the file

        A. Method 1. Use the position pointed by the current SEEK_CUR pointer as the center.

        fseek(pf, 2, SEEK_CUR);//This code means: point the file pointer to the character position pointed by the current pointer, because in exercise 1, after the file pointer is read, the pointer automatically jumps to the next character position d, the current offset 0 represents d, the offset is set to 2, and 2 represents the character f.

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

        fseek(pf, 5, SEEK_SET);//This code means: point the file pointer to the beginning of the file, the current offset 0 represents a, the offset is set to 5, and 5 represents the character f.

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

        fseek(pf, -1, SEEK_END);//It means: point the file pointer to the end of the file content, the file content is abcdef, the end position is the position after the character f, the offset is -1, and the pointer moves forward one position so that the pointer can point to character f.


2. ftell function

long int ftell ( FILE * stream );

1. Function:

         Returns the offset of the file pointer relative to the starting position.

2. Code practice:

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);//ch接收文件指针指向的内容,c字符
	printf("%c\n", ch);
	printf("%d\n",ftell(pf));//3


	fseek(pf, -1, SEEK_END);
	ch = fgetc(pf);//ch接收文件指针指向的内容,f字符
	printf("%c\n", ch);
	printf("%d\n", ftell(pf));//



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

 

The debugging results are as follows:

 

Code explanation: The file opening method is: read file 

                The reason why the result is 3: because ch receives the character c at the position pointed to by pf. After output, the file pointer will automatically jump to the position of the next character d. The ftell function calculates the offset of the current position of the pointer relative to the beginning of the file. distance.

                The reason why the result is 6: After ch receives the character f pointed to by the file pointer, it automatically moves backward and points to the end position. The ftell function calculates the offset distance of the end position relative to the beginning of the file to be 6, and the offset distance is 0 represents the first file content a; the offset distance is 5, which represents the sixth content character f in the file content.


3.rewind function

 

1. Function:

Return the file pointer to the beginning of the file.

2. Code practice:

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);//ch接收文件指针指向的内容,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);//ch接收文件指针指向的内容,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;
}

 

 

Okay, that’s it for the functions and usage of these three functions. If you find it useful, please remember to click three in a row~ See you in the next issue!

Guess you like

Origin blog.csdn.net/weixin_69283129/article/details/126164935