Home  >  Article  >  What is the function of rewind

What is the function of rewind

青灯夜游
青灯夜游Original
2020-12-18 17:00:5113224browse

函数rewind的作用是使文件位置指针重新返回文件的开始位置。函数rewind的功能是将文件指针重新指向一个流的开头,基本语法是“void rewind(FILE * stream)”。

What is the function of rewind

本教程操作环境:Windows10系统、Dell G3电脑。

函数rewind的作用是:使文件位置指针重新返回文件的开始位置。

C 库函数 - rewind()

功 能: 将文件内部的位置 指针重新指向一个流( 数据流/文件)的开头

注意:不是 文件指针而是文件内部的位置指针,随着对文件的读写文件的位置指针(指向当前读写字节)向后移动。而文件指针是指向整个文件,如果不重新赋值文件指针不会改变。

用 法: 

void rewind(FILE *stream);

头文件:  stdio.h

返回值:无

例如:从键盘输入一行字符,追加写入到一个文件中,再把该文件内容读出显示在屏幕上。

#includeade979de5fc0e1ca0540f360a64c230b
 
int main()
{
    FILE  *fp;
    char  ch;
    if((fp=fopen("C:\\Users\\dell\\Desktop\\abc.txt","ab+"))==NULL)
    {
        printf("\nCannot open file\nstrike any key exit\n");
        getchar();
        return 1;
    }
    printf("input a string:\n");
    ch=getchar();
    while(ch!='\n')
    {
        fputc(ch,fp);
        ch=getchar();
    }
    rewind(fp);        
    ch=fgetc(fp);          
    while(ch!=EOF)
    {
        putchar(ch);
        ch=fgetc(fp);
    }
    fclose(fp);
    return 0;
}

程序第20行,rewind(fp);每输入一个字符,文件内部位置指针向后移动一个字节。写入完毕,该指针已指向文件末尾, 如果要把文件从头读出,须把指针移到文件头,利用rewind()函数。

运行结果:

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of What is the function of rewind. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn