w3resource

C rewind() function

C library function - rewind()

The rewind() function is used to reposition the file pointer associated with stream to the beginning of the file.

Syntax:

void rewind(FILE *stream)

rewind() Parameters:

Name Description Required /Optional
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream. Required

Return value from rewind()

  • This function does not return any value.

A call to the rewind() function is the same as:

(void) fseek(stream, 0L, SEEK_SET)

except that rewind() shall also clear the error indicator.

Example: rewind() function

Following example first opens a file test.txt for input and output. It writes integers to the file, uses rewind() to reposition the file pointer to the beginning of the file, and then reads in the data.

#include <stdio.h>
FILE *stream; 
int p, q, r, s;
int main(void)
{
    p = 100; q = 200;
    /* Input data in the file */
   stream = fopen("test.txt", "w+");
   fprintf(stream, "%d %d\n", p, q);
    /* Now read the data file */
   rewind(stream);
   fscanf(stream, "%d", &r);
   fscanf(stream, "%d", &s);
   printf("Latest values are: %d and %d\n", r, s);
}

Output:

Latest values are: 100 and 200

C Programming Code Editor:

Previous C Programming: C rewind()
Next C Programming: C setbuf()



Share this Tutorial / Exercise on : Facebook and Twitter

C Programming: Tips of the Day

What's an object file in C?

An object file is the real output from the compilation phase. It's mostly machine code, but has info that allows a linker to see what symbols are in it as well as symbols it requires in order to work. (For reference, "symbols" are basically names of global objects, functions, etc.)

A linker takes all these object files and combines them to form one executable (assuming that it can, i.e.: that there aren't any duplicate or undefined symbols). A lot of compilers will do this for you (read: they run the linker on their own) if you don't tell them to "just compile" using command-line options. (-c is a common "just compile; don't link" option.)

Ref : https://bit.ly/3CbzF8M