曲径通幽论坛

标题: rewind() -- 将文件流指针调整到开始处 [打印本页]

作者: beyes    时间: 2012-8-14 18:50
标题: rewind() -- 将文件流指针调整到开始处
rewind() 函数原型如下:
[C++] 纯文本查看 复制代码
#include <stdio.h>
void rewind(FILE *stream);

该函数将文件流指针(stream)调整到开始处。

测试代码:
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <string.h>

int main(void)
{
        FILE *fp;
        char *str1 = "hello";
        char *str2 = "linux";

        char buf[32];
        bzero (buf, sizeof(buf));

        fp = fopen("test.txt", "w+");
        fprintf (fp, "%s %s", str1, str2);
        
        fseek(fp, -strlen(str2), SEEK_CUR);
        fscanf (fp, "%s", buf);
        printf ("%s\n", buf);
        
        rewind(fp);
        fscanf (fp, "%s", buf);
        printf ("%s\n", buf);

        return 0;
}

运行输出:
$ ./rewind
linux
hello
注意,fscanf() 函数读取字符串时,在遇到第一个空格时停止读入。




欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) Powered by Discuz! X3.2