C 庫函數(shù) - rewind()

C 標(biāo)準(zhǔn)庫 - <stdio.h> C 標(biāo)準(zhǔn)庫 - <stdio.h>

描述

C 庫函數(shù) void rewind(FILE *stream) 設(shè)置文件位置為給定流 stream 的文件的開頭。

聲明

下面是 rewind() 函數(shù)的聲明。

void rewind(FILE *stream)

參數(shù)

  • stream -- 這是指向 FILE 對象的指針,該 FILE 對象標(biāo)識了流。

返回值

該函數(shù)不返回任何值。

實例

下面的實例演示了 rewind() 函數(shù)的用法。

#include <stdio.h>

int main()
{
   char str[] = "This is w3cschool.cn";
   FILE *fp;
   int ch;

   /* 首先讓我們在文件中寫入一些內(nèi)容 */
   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);
}

假設(shè)我們有一個文本文件 file.txt,它的內(nèi)容如下:

This is w3cschool.cn

讓我們編譯并運行上面的程序,這將產(chǎn)生以下結(jié)果:

This is w3cschool.cn
This is w3cschool.cn

C 標(biāo)準(zhǔn)庫 - <stdio.h> C 標(biāo)準(zhǔn)庫 - <stdio.h>