C File Pointers

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is File Pointers?

A FILE * identifies an open stream. Three are open automatically: stdin (input), stdout (output), and stderr (error output). The same functions work on all of them, for example printf(x) is fprintf(stdout, x). Each stream remembers a position indicator that advances as you read or write.

Remember
FILE *fp;                       /* declare */
fprintf(stdout, "to screen\n");  /* same as printf */
fprintf(stderr, "to errors\n");  /* unbuffered errors */
fgets(buf, n, stdin);            /* from keyboard */

Cheat Sheet

Keep these facts handy while you work with File Pointers in C.

TopicDetail
stdinStandard input
stdoutStandard output, buffered
stderrStandard error, unbuffered
PositionMoves with each read/write

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Opening and Closing Files.

Example 1 — The standard streams are FILE pointers

Use the same function for the screen or a file.

C
#include <stdio.h>

static void report(FILE *out, const char *msg) {
    fprintf(out, "%s\n", msg);
}

int main(void) {
    report(stdout, "writing through a FILE *");
    printf("stdout is a FILE *: %d\n", stdout != NULL);

    FILE *fp = fopen("log.txt", "w");
    if (fp) {
        report(fp, "saved to a file");
        fclose(fp);
        printf("log.txt written\n");
    }
    return 0;
}

How It Works

  1. report accepts any FILE *, whether screen or disk.
  2. The first call writes to stdout.
  3. The second writes the same text into log.txt.

Edge Cases & Pitfalls

Check these before relying on File Pointers in real programs.

copy

Copying FILE objects

Never copy a FILE struct; always pass the pointer.

stderr

Mixing stdout and stderr

They are separate streams, so output may interleave unexpectedly when both go to a terminal.

Key Takeaways

  • FILE *: handle to a stream.
  • Three defaults: stdin, stdout, stderr.
  • Reuse: same functions everywhere.

One line: A FILE * identifies an open stream.

Frequently Asked Questions

They are the standard input, output, and error streams that are opened automatically when a program starts.
printf(fmt, ...) behaves like fprintf(stdout, fmt, ...).
Continue with "C Opening and Closing Files" next. Call fopen, test for NULL, and always fclose. The C File Handling hub lists every topic in order.

Next: Opening and Closing Files

Call fopen, test for NULL, and always fclose.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful