c文件 fsetpos_使用示例的C语言中的fsetpos()函数

本文介绍了C语言中的文件处理函数fsetpos(),用于设置文件流指示器的位置。fsetpos()结合fgetpos()使用,能够帮助在文件中精确地定位读取或写入的位置。示例代码展示了如何使用fsetpos()成功操作文件并处理错误情况。
摘要由CSDN通过智能技术生成

c文件 fsetpos

C中的fsetpos()函数 (fsetpos() function in C)

Prototype:

原型:

    int fsetpos(FILE* filename, fpos_t *position);

Parameters:

参数:

    FILE* filename, fpos_t *position

Return type: int

返回类型: int

Use of function:

使用功能:

In file handling, through the fsetpos() function we set the position of the input file stream indicator at the point which we get from fgetpos(). Whenever we need to fix the file indicator position in the file, we need to use the function fgetpos(). The prototype of the fgetpos() function is:

在文件处理中,通过fsetpos()函数,我们将输入文件流指示器的位置设置为从fgetpos()得到的点。 每当需要固定文件指示符在文件中的位置时,就需要使用函数fgetpos()fgetpos()函数的原型为:

    int fsetpos(FILE* filename, fpos_t *position);

Here, the data type of the position variable must be fpos_t type. A return value of zero means the successful operation and non-zero returns means failure.

在此,位置变量的数据类型必须为fpos_t类型。 返回值为零表示操作成功,非零返回值表示失败。

C中的fsetpos()示例 (fsetpos() example in C)

#include <stdio.h>
#include <stdlib.h>

int main(){
	//Initialize the file pointer
	FILE *f;
	//Take a array of characters 
	char ch[100];
	//Initialize the position variable 
	fpos_t pos;

	//Create the file for write operation
	f=fopen("includehelp.txt","w+");
	//Store the value of the function point indicator
	fgetpos(f,&pos);
	printf("Enter five strings\n");
	for(int i=0;i<4;i++){
		//take the strings from the users
		scanf("%[^\n]",&ch);
		//write back to the file
		fputs(ch,f);
		//every time take a new line for the new entry string 
		//except for last entry.Otherwise print the last line twice
		fputs("\n",f);
		//clear the stdin stream buffer
		//if we don't write this then after taking string 
		//%[^\n] is waiting for the '\n' or white space
		fflush(stdin);
	}
	//take the strings from the users
	scanf("%[^\n]",&ch);
	fputs(ch,f);
	//set the indicator position to the initial position of the file
	fsetpos(f,&pos);
	printf("\n...............print the strings..............\n\n");
	while(!feof(f)){
		//takes the first 100 character in the character array 
		fgets(ch,100,f);
		//and print the strings
		printf("%s",ch);
	}

	//close the file
	fclose(f);

	return 0;
}

Output

输出量

fsetpos example in c

翻译自: https://www.includehelp.com/c-programs/fsetpos-function-in-c-language-with-example.aspx

c文件 fsetpos

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值