gtpc2m5w | C/C++ Language Support User's Guide |
This function sets file position to the beginning of the file.
Format
#include <stdio.h> void rewind(FILE *stream);
This function repositions the file position indicator of the stream pointed to by stream. A call to the rewind function is the same as the statement that follows except that rewind also clears the error indicator for the stream.
(void) fseek(stream, 0L, SEEK_SET);
Normal Return
There is no returned value.
Error Return
If an error occurs, errno is set. After the error, the file position does not change. The next operation can be either a read or a write operation.
Programming Considerations
None.
Examples
The following example first opens a myfile.dat file for input and output. It writes integers to the file, uses the rewind function to reposition the file pointer to the beginning of the file, and then reads back the data.
#include <stdio.h> int main(void) { FILE *stream; int data1, data2, data3, data4; data1 = 1; data2 = -37; /* Place data in the file */ stream = fopen("myfile.dat", "w+"); fprintf(stream, "%d %d\n", data1, data2); /* Now read the data file */ rewind(stream); fscanf(stream, "%d", &data3); fscanf(stream, "%d", &data4); printf("The values read back in are: %d and %d\n", data3, data4); }
Output
The values read back in are: 1 and -37
Related Information
See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.