rewind() in C

The rewind() function is used to bring the file pointer to the beginning of the file. It can be used in place of fseek() when you want the file pointer at the start.

Syntax of rewind()

rewind (file_pointer);

Example

C




// C program to illustrate the use of rewind
#include <stdio.h>
 
int main()
{
    FILE* fptr;
    fptr = fopen("file.txt", "w+");
    fprintf(fptr, "Geeks for Geeks\n");
 
    // using rewind()
    rewind(fptr);
 
    // reading from file
    char buf[50];
    fscanf(fptr, "%[^\n]s", buf);
 
    printf("%s", buf);
 
    return 0;
}


Output

Geeks for Geeks

Basics of File Handling in C

File handing in C is the process in which we create, open, read, write, and close operations on a file. C language provides different functions such as fopen(), fwrite(), fread(), fseek(), fprintf(), etc. to perform input, output, and many different C file operations in our program.

Similar Reads

Why do we need File Handling in C?

So far the operations using the C program are done on a prompt/terminal which is not stored anywhere. The output is deleted when the program is closed. But in the software industry, most programs are written to store the information fetched from the program. The use of file handling is exactly what the situation calls for....

Types of Files in C

A file can be classified into two types based on the way the file stores the data. They are as follows:...

C File Operations

C file operations refer to the different possible operations that we can perform on a file in C such as:...

Functions for C File Operations

...

File Pointer in C

A file pointer is a reference to a particular position in the opened file. It is used in file handling to perform all file operations such as read, write, close, etc. We use the FILE macro to declare the file pointer variable. The FILE macro is defined inside header file....

Open a File in C

For opening a file in C, the fopen() function is used with the filename or file path along with the required access modes....

Create a File in C

...

Reading From a File

The fopen() function can not only open a file but also can create a file if it does not exist already. For that, we have to use the modes that allow the creation of a file if not found such as w, w+, wb, wb+, a, a+, ab, and ab+....

Write to a File

...

Closing a File

The file read operation in C can be performed using functions fscanf() or fgets(). Both the functions performed the same operations as that of scanf and gets but with an additional parameter, the file pointer. There are also other functions we can use to read from a file. Such functions are listed below:...

Examples of File Handing in C

The file write operations can be performed by the functions fprintf() and fputs() with similarities to read operations. C programming also provides some other functions that can be used to write data to a file such as:...

Read and Write in a Binary File

The fclose() function is used to close the file. After successful file operations, you must always close a file to remove it from the memory....

fseek() in C

Example 1: Program to Create a File, Write in it, And Close the File...

rewind() in C

...

More Functions for C File Operations

...