C 库函数 - rewind()

C 语言教程 · 2019-02-23 17:14:07

描述

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

声明

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

void rewind(FILE *stream)

参数

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

返回值

该函数不返回任何值。

实例

下面的实例演示了 rewind() 函数的用法。

#include <stdio.h>

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

   /* 首先让我们在文件中写入一些内容 */
   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 codercto.com

让我们编译并运行上面的程序,这将产生以下结果:

This is codercto.com
This is codercto.com

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Head First Design Patterns

Head First Design Patterns

Elisabeth Freeman、Eric Freeman、Bert Bates、Kathy Sierra、Elisabeth Robson / O'Reilly Media / 2004-11-1 / USD 49.99

You're not alone. At any given moment, somewhere in the world someone struggles with the same software design problems you have. You know you don't want to reinvent the wheel (or worse, a flat tire),......一起来看看 《Head First Design Patterns》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具