目录

void rewind(FILE *stream)

描述 (Description)

C库函数void rewind(FILE *stream)将文件位置设置为给定stream文件的开头。

声明 (Declaration)

以下是rewind()函数的声明。

void rewind(FILE *stream)

参数 (Parameters)

  • stream - 这是指向标识流的FILE对象的指针。

返回值 (Return Value)

此函数不返回任何值。

例子 (Example)

以下示例显示了rewind()函数的用法。

#include <stdio.h>
int main () {
   char str[] = "This is iowiki.com";
   FILE *fp;
   int ch;
   /* First let's write some content in the file */
   fp = fopen( "file.txt" , "w" );
   fwrite(str , 1 , sizeof(str) , fp );
   fclose(fp);
   fp = fopen( "file.txt" , "r" );
   while(1) {
      ch = fgetc(fp);
      if( feof(fp) ) {
         break ;
      }
      printf("%c", ch);
   }
   rewind(fp);
   printf("\n");
   while(1) {
      ch = fgetc(fp);
      if( feof(fp) ) {
         break ;
      }
      printf("%c", ch);
   }
   fclose(fp);
   return(0);
}

我们假设我们有一个文本文件file.txt ,其中包含以下内容 -

This is iowiki.com

现在让我们编译并运行上面的程序来产生以下结果 -

This is iowiki.com
This is iowiki.com
↑回到顶部↑
WIKI教程 @2018