Reset File Reading to the Beginning in C

To reset file reading to the beginning in C, you can use functions such as fseek(), rewind(), or even freopen() to reposition the file pointer. These methods allow you to start reading the file from the beginning again.


Example 1: Using fseek() to Reset the File Pointer

In this example, we will open a file, read some content, then use fseek() to set the file pointer back to the beginning so that we can re-read the content from the start.

main.c

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        exit(1);
    }

    char ch;
    
    // Read and print first 10 characters
    int count = 0;
    while ((ch = fgetc(file)) != EOF && count < 10) {
        printf("%c", ch);
        count++;
    }
    printf("\n");

    // Reset the file pointer to the beginning using fseek()
    fseek(file, 0, SEEK_SET);

    // Read and print first 10 characters again
    count = 0;
    while ((ch = fgetc(file)) != EOF && count < 10) {
        printf("%c", ch);
        count++;
    }
    printf("\n");

    fclose(file);
    return 0;
}

Explanation:

  1. The file example.txt is opened in read mode and assigned to the file pointer file.
  2. The program reads and prints the first 10 characters using fgetc() in a loop.
  3. fseek(file, 0, SEEK_SET) resets the file pointer to the beginning of the file.
  4. The file is read again from the beginning, printing the same 10 characters.
  5. Finally, the file is closed using fclose().

Output:

First10Chars
First10Chars

Example 2: Using rewind() to Reset the File Pointer

In this example, we will open a file, read a line from it, and then use rewind() to reset the file pointer to the beginning. This allows us to re-read the line or other parts of the file.

main.c

</>
Copy
#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *file = fopen("example.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        exit(1);
    }

    char line[100];
    
    // Read and print a line from the file
    if (fgets(line, sizeof(line), file) != NULL) {
        printf("First read: %s", line);
    }

    // Reset the file pointer to the beginning using rewind()
    rewind(file);

    // Read and print the same line again
    if (fgets(line, sizeof(line), file) != NULL) {
        printf("Second read: %s", line);
    }

    fclose(file);
    return 0;
}

Explanation:

  1. The file example.txt is opened and assigned to the pointer file.
  2. A line is read from the file using fgets() and stored in the character array line.
  3. The rewind(file) function resets the file pointer to the beginning of the file.
  4. The same line is read again from the beginning of the file using fgets().
  5. The file is closed using fclose() after the operations.

Output:

First read: This is a sample line from the file.
Second read: This is a sample line from the file.