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

查看所有标签

Spring 2企业应用开发

Spring 2企业应用开发

人民邮电出版社 / 2008-10 / 45.00元

《Spring 2企业应用开发》是一部权威的实战指南,由Spring开发团队撰写,全面讲述了Spring2企业应用开发。主要涵盖SpringFramework、核心容器、AOP(面向方面编程)、典型的SpringAOP框架等内容。通过阅读《Spring 2企业应用开发》,读者能够在实战中掌握Spring最佳实践,成为高效的Java开发人员。一起来看看 《Spring 2企业应用开发》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具