C语言 <stdio.h> rewind() 函数

  • 描述

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

    以下是rewind()函数的声明。
    
    void rewind(FILE *stream)
    
    参数
    • stream - 这是指向标识流的FILE对象的指针。
  • 返回值

    此函数不返回任何值。
    示例
    以下示例显示rewind()函数的用法-
    
    #include <stdio.h>
     
    
    int main () {
       char str[] = "This is jc2182.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 jc2182.com
    
    现在让我们编译并运行上述程序以产生以下结果-
    
    This is jc2182.com
    This is jc2182.com