c语言中rewind函数_C语言中的rewind()函数与示例

c语言中rewind函数

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

The rewind() function is defined in the <stdio.h> header file.

rewind()函数在<stdio.h>头文件中定义。

Prototype:

原型:

    void rewind(FILE *filename);

Parameters: FILE *filename

参数: FILE *文件名

Return type: void

返回类型: void

Use of function:

使用功能:

When we are dealing with files then sometimes we need to start of the specified files. In file handling, we use rewind() function to move the file position indicator to start of the specified file stream. The prototype of the function rewind() is void rewind(FILE *filename);

当我们处理文件时,有时我们需要启动指定的文件。 在文件处理中,我们使用rewind()函数将文件位置指示符移动到指定文件流的开始。 函数rewind()原型为void rewind(FILE * filename);

Here, filename is the name of the file where the file indicator starts. By the function, end-of-file and error flag is cleared.

在这里, filename是文件指示符开始处的文件名。 通过该功能,文件结束和错误标志被清除。

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

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

int main(){
	//Initialize the file pointer
	FILE *f;
	char ch[100];

	//Create the file for write operation
	f=fopen("includehelp.txt","w");
	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
		fflush(stdin);
	}
	//take the strings from the users
	scanf("%[^\n]",&ch);
	fputs(ch,f);
	//close the file after write operation is over
	fclose(f);

	//open a file
	f=fopen("includehelp.txt","r");
	printf("\n...............print the strings..............\n");
	while(!feof(f)){
		//takes the first 100 character in the character array 
		fgets(ch,100,f);
		//and print the strings
		printf("%s",ch);
	}
	rewind(f);
	printf("\n...............print the strings again..............\n");
	while(!feof(f)){
		fgets(ch,100,f);
		printf("%s",ch);
	}

	//close the file
	fclose(f);

	return 0;
}

Output

输出量

rewind() example in C language

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

c语言中rewind函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值