October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Use rewind() in C to Read a File Again

Use rewind() to reposition a C file stream at the beginning and clear its end-of-file and error indicators. Here’s how to reread a file and when fseek() is the better choice.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Call rewind(stream) to move a C file stream back to the beginning before reading it again. The function also clears the stream’s end-of-file and error indicators, but it returns no value—so you cannot check success with a return-value test.

What rewind() does

Declare it by including <stdio.h>. Its signature is void rewind(FILE *stream);. It moves the file-position indicator for stream to the beginning of the file and clears the end-of-file and error indicators. Any effects from earlier ungetc() calls are discarded. The C library reference documents these semantics.

As an Amazon Associate I earn from qualifying purchases.

Because its return type is void, rewind() does not report success or failure through a return value.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How to read a file from the beginning again

Keep the stream open, finish the first pass, rewind it, then perform the next read. For example:

#include <stdio.h>

int main(void)
{
    FILE *stream = fopen("input.txt", "r");
    if (stream == NULL) {
        perror("fopen");
        return 1;
    }

    /* Read or process the stream here. */

    rewind(stream);
    /* Read or process the stream again from the beginning. */

    if (fclose(stream) == EOF) {
        perror("fclose");
        return 1;
    }
    return 0;
}

The comments mark where the program’s actual reading logic belongs. The example demonstrates the intended sequence; it does not establish that repositioning succeeds for every stream or platform situation.

rewind() vs. fseek()

Both rewind(stream) and fseek(stream, 0L, SEEK_SET) can position a stream at its beginning. Choose based on whether you need a fixed destination or more control over positioning and status reporting.

Function Positioning Return value Indicator effects
rewind(stream) Beginning of the file only None (void) Clears the end-of-file and error indicators; discards effects from prior ungetc() calls. C library reference
fseek(stream, offset, whence) Chosen offset relative to SEEK_SET (start), SEEK_CUR (current position), or SEEK_END (end) Reports status Unlike rewind(), does not provide its additional error-indicator clearing. POSIX.1-2017

Use rewind() when the only destination you need is the start. Use fseek() when you need another position or want to inspect a function return status. The C library reference for fseek() describes its offset and origin arguments.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Error detection and portability

Do not write if (rewind(stream) != 0): there is no return value to test. POSIX.1-2017 says that an application wishing to detect errors may clear errno before calling rewind(), then treat a nonzero errno afterward as an indication of an error. This is POSIX guidance, not a return-value check, and should not be presented as a portable C success test. See the POSIX.1-2017 specification.

Best Value

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.