在 C 中,fseek() 应该优先于 rewind()。
请注意以下文本 C99 标准:
rewind 函数将 stream 指向的流的文件位置指示符设置为文件的开头。它相当于
除了流的错误指示符也被清除。
以下代码示例使用 rewind() 将输入流的文件位置指示符设置回开头。但是没有办法检查 rewind() 是否成功。
在上面的代码中,可以使用 fseek() 代替 rewind() 来查看操作是否成功。可以使用以下代码行代替 rewind(fp);
(void)fseek(stream, 0L, SEEK_SET)
int main()
{
FILE *fp = fopen("test.txt", "r");
if ( fp == NULL ) {
/* Handle open error */
}
/* Do some processing with file*/
rewind(fp); /* no way to check if rewind is successful */
/* Do some more precessing with file */
return 0;
}
if ( fseek(fp, 0L, SEEK_SET) != 0 ) {
/* Handle repositioning error */
}