0
0
CHow-ToBeginner · 3 min read

How to Redirect stdin and stdout in C: Simple Guide

In C, you can redirect stdin and stdout using the freopen function to associate these streams with files. For example, freopen("input.txt", "r", stdin) redirects input from a file, and freopen("output.txt", "w", stdout) redirects output to a file.
📐

Syntax

The freopen function is used to redirect standard streams. Its syntax is:

FILE *freopen(const char *filename, const char *mode, FILE *stream);
  • filename: The file to open for input or output.
  • mode: The mode to open the file in, e.g., "r" for reading, "w" for writing.
  • stream: The standard stream to redirect, such as stdin or stdout.

This replaces the given stream with the file stream, so input or output goes to the file instead of the console.

c
freopen("filename.txt", "r", stdin);
freopen("filename.txt", "w", stdout);
💻

Example

This example shows how to redirect stdin to read from a file and stdout to write to a file. It reads a line from the input file and writes it to the output file.

c
#include <stdio.h>

int main() {
    // Redirect stdin to read from input.txt
    if (freopen("input.txt", "r", stdin) == NULL) {
        perror("Failed to redirect stdin");
        return 1;
    }

    // Redirect stdout to write to output.txt
    if (freopen("output.txt", "w", stdout) == NULL) {
        perror("Failed to redirect stdout");
        return 1;
    }

    char buffer[100];
    // Read a line from redirected stdin (input.txt)
    if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
        // Write the line to redirected stdout (output.txt)
        printf("Read line: %s", buffer);
    }

    return 0;
}
⚠️

Common Pitfalls

  • Not checking if freopen returns NULL, which means the file could not be opened.
  • Redirecting streams multiple times without closing them can cause unexpected behavior.
  • Forgetting to flush stdout before program ends may cause output not to appear in the file.
  • Using wrong mode strings (e.g., "r" for writing) causes errors.

Always check the return value of freopen and use correct modes.

c
#include <stdio.h>

int main() {
    // Wrong mode example - trying to write with "r" mode
    if (freopen("output.txt", "r", stdout) == NULL) {
        perror("Failed to redirect stdout with wrong mode");
    }

    // Correct way
    if (freopen("output.txt", "w", stdout) == NULL) {
        perror("Failed to redirect stdout");
    }

    printf("This will be written to output.txt\n");
    return 0;
}
📊

Quick Reference

Summary tips for redirecting stdin and stdout in C:

  • Use freopen to redirect streams to files.
  • Check if freopen returns NULL to handle errors.
  • Use mode "r" for reading input files and "w" for writing output files.
  • Flush stdout with fflush(stdout) if needed before program ends.
  • Redirect streams early in main before using scanf or printf.

Key Takeaways

Use freopen to redirect stdin or stdout to a file in C.
Always check if freopen returns NULL to catch file open errors.
Use correct mode strings: "r" for reading, "w" for writing.
Flush stdout if you want to ensure output is written immediately.
Redirect streams before performing input/output operations.