MSDN Documentation – Windows

File Pointers

A file pointer in the Windows API, represented by a FILE* handle in the C Runtime Library, tracks the current read/write position within an open file stream. It is central to sequential and random access operations.

Key Functions

Using File Pointers – Example

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

int main(void) {
    FILE *fp = fopen("example.txt", "w+");
    if (!fp) {
        perror("fopen");
        return EXIT_FAILURE;
    }

    // Write data
    fputs("Hello, world!\n", fp);
    fputs("File pointers are useful.\n", fp);

    // Move to the beginning
    rewind(fp);

    // Read and display
    char buffer[64];
    while (fgets(buffer, sizeof(buffer), fp)) {
        puts(buffer);
    }

    // Seek to the second line (offset after first line)
    fseek(fp, 14, SEEK_SET); // 14 bytes offset
    fputs("INSERTED LINE\n", fp);

    // Clean up
    fclose(fp);
    return EXIT_SUCCESS;
}

Best Practices

Related Topics