In C, fseek() should be preferred over rewind().
Note the following text C99 standard:
The rewind function sets the file position indicator for the stream pointed to by stream to the beginning of the file. It is equivalent to
except that the error indicator for the stream is also cleared.
This following code example sets the file position indicator of an input stream back to the beginning using rewind(). But there is no way to check whether the rewind() was successful.
In the above code, fseek() can be used instead of rewind() to see if the operation succeeded. Following lines of code can be used in place of rewind(fp);
This article is contributed by Rahul Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Basics of File Handling in C
- Read/Write structure to a file in C
- fseek() in C/C++ with example
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- C Program to print numbers from 1 to N without using semicolon?
- Enumeration (or enum) in C
- Commonly Asked C Programming Interview Questions | Set 1
- fopen() for an existing file in write mode
- G-Fact 17
- How to deallocate memory without using free() in C?
- How does free() know the size of memory to be deallocated?
- EOF, getc() and feof() in C
- What is Memory Leak? How can we avoid?
- Write a C macro PRINT(x) which prints x
- Understanding "extern" keyword in C