码迷,mamicode.com
首页 > Windows程序 > 详细

C library function - rewind()

时间:2015-11-30 22:04:38      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

Description

The C library function void rewind(FILE *stream) sets the file position to the beginning of the file of the given stream.

Declaration

Following is the declaration for rewind() function.

void rewind(FILE *stream)

Parameters

  • stream − This is the pointer to a FILE object that identifies the stream.

Return Value

This function does not return any value.

Example

The following example shows the usage of rewind() function.

#include <stdio.h>

int main()
{
   char str[] = "This is tutorialspoint.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);
   }

  //set the file position to the beginning, if it doesn‘t have this function, fp will point NULL ,because above while(1) and break when feof(fp) is true. rewind(fp); printf("\n"); while(1) { ch = fgetc(fp); if( feof(fp) ) { break ; } printf("%c", ch); } fclose(fp); return(0); }

Let us assume we have a text file file.txt that have the following content −

This is tutorialspoint.com

Now let us compile and run the above program to produce the following result −

This is tutorialspoint.com
This is tutorialspoint.com

C library function - rewind()

标签:

原文地址:http://www.cnblogs.com/zhonghuasong/p/5008387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!