C language rewind () function: Resets the file pointer to the beginning of the file

Header file: #include <stdio.h>

rewind () function is used at the beginning of the file pointer pointing to the file, and clears the file stream associated the eof and error, equivalent to calling fseek (stream, 0, SEEK_SET), which prototype is as follows:

void rewind(FILE * stream);

Parameters stream is to open the file pointer.

Note: exact, this is not a pointer file pointer stream, but the position of the pointer inside the file, file read and write pointers as the position of the document (pointing to the current read and write bytes) moves backward. The file pointer is a pointer to the entire file, if the file pointer is not re-assignment will not change.

FILE * stream file pointer in a read-write location pointers comprising char * _nextc, it points to the next write location of the file. The following structure:

typedef struct
{
    int _fd;  // 文件号
    int _cleft;  // 缓冲区中剩下的字节数
    int _mode;  // 文件操作模式
    char * _nextc;  // 下一个字节的位置
    char * _buff;  // 文件缓冲区位置
}FILE;

Whenever write once, this pointer points to the next read automatically position. When the file just opened or created, the pointer to the beginning of the file. Can be obtained by a function ftell () current position pointer, the position pointer may be changed by rewind () / fseek () function, the reader needs to point to the location.

Examples of data [] to read the file back to the beginning and then re-read.

#include<iostream.h>
#include<stdio.h>
void main(void)
{
    FILE* stream;
    long l;
    float fp;
    char s[81];
    char c;
    stream = fopen("fscanf.txt","w+");
    if(stream == NULL)/*打开文件失败*/
    {
        printf("the file is opeaned error!\n");
    }
    else/*成功则输出信息*/
    {
        fprintf(stream,"%s %ld %f %c","a_string",6500,3.1415,'x');
        fseek(stream,0L,SEEK_SET);            /*定位文件读写指针*/
        fscanf(stream,"%s",s);
        printf("%ld\n",ftell(stream));
        fscanf(stream,"%ld",&l);
        printf("%ld\n",ftell(stream));
        fscanf(stream,"%f",&fp);
        printf("%ld\n",ftell(stream));
        fscanf(stream," %c",&c);
        printf("%ld\n",ftell(stream));
        rewind(stream);/*指向文件开头*/
        fscanf(stream,"%s",s);
        printf("%s\n",s);
        fclose(stream);/*关闭流*/
    }
}
运行结果:
8
13
22
24
a_string

Program to create a file write some data, and then use the function feeek locate the file pointer to the beginning of the file one by one to read the data, use the function rewind the file read / write pointer to reposition the beginning of the file, read again after the completion of the reading It found that reading is the beginning of the character a_string.

As another example, the contents of a file displayed on a screen, and simultaneously copied to another document.

#include "stdio.h"
void main()
{
    FILE *fp1, *fp2;
    fp1 = fopen("file1.c", "r");  // 源文件
    fp2 = fopen("file2.c", "w");  // 复制到file2.c
    while(!feof(fp1)) putchar(fgetc(fp1));  // 显示到屏幕上
    rewind(fp1);   // fp回到开始位置
    while(!feof(fp1)) fputc(fgetc(fp1), fp2);
    fclose(fp1);
    fclose(fp2);
}

Guess you like

Origin blog.csdn.net/u013693952/article/details/93460908