C语言教程:rewind()函数(将文件指针设置在流的开头)

2018-10-0906:47:42编程语言入门到精通Comments2,883 views字数 624阅读模式

rewind()函数将文件指针设置在流的开头。在需要多次使用流时,这就很有用。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6470.html

rewind()函数的语法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6470.html

void rewind(FILE *stream)
C

示例:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6470.html

创建一个源文件:rewind-file.c,其代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6470.html

#include<stdio.h>   
void main() {
    FILE *fp;
    char c;

    fp = fopen("string-file.txt", "r");
    while ((c = fgetc(fp)) != EOF) {
        printf("%c", c);
    }

    rewind(fp); // moves the file pointer at beginning of the file  
    // 不用重新打开文件,直接从头读取内容
    while ((c = fgetc(fp)) != EOF) {
        printf("%c", c);
    }

    fclose(fp);

}
C

创建一个文本文件:string-file.txt,内容如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6470.html

this is rewind()function from yiibai tutorials.
Txt

执行上面示例代码后,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6470.html

this is rewind()function from yiibai tutorials.
this is rewind()function from yiibai tutorials.
Shell

如上所示,rewind()函数将文件指针移动到文件的开头,这就是为什么文件string-file.txt中的内容被打印2次。 如果不调用rewind()函数,文件中的内容将只打印一次。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6470.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6470.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定