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;
}
运行输出:注意,fscanf() 函数读取字符串时,在遇到第一个空格时停止读入。 |