Ch11.11.1: Complete C I/O Function Reference

File Opening and Closing

Function Description
fopenOpen a file
fcloseClose a file
fflushFlush a stream’s buffer

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>

int main() {
    // Using c_file for RAII
    ::fast_io::c_file cf(u8"test.txt", ::fast_io::open_mode::out);
    std::fprintf(cf.fp, "%s", "Hello, World!\n");
    std::fflush(cf.fp);  // Force write to disk
    // File is automatically closed when cf goes out of scope
    return 0;
}

Formatted Output

Function Description
printfFormatted output to stdout
fprintfFormatted output to a stream
sprintfFormatted output to a string buffer
snprintfFormatted output to a string buffer with size limit
vprintfFormatted output to stdout (variadic)
vfprintfFormatted output to a stream (variadic)
vsprintfFormatted output to a string buffer (variadic)
vsnprintfFormatted output to a string buffer with size limit (variadic)

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>

int main() {
    int x{42};
    double pi{3.14159};

    // printf to stdout (format string has only specifiers, all literals in args)
    std::printf("%s%d%s%.2f%s", "x = ", x, ", pi = ", pi, "\n");

    // fprintf to file (with RAII)
    ::fast_io::c_file cf(u8"output.txt", ::fast_io::open_mode::out);
    std::fprintf(cf.fp, "%s%d%s", "x = ", x, "\n");

    // sprintf to string buffer
    char buffer[100];
    std::sprintf(buffer, "%s%d", "Value: ", x);

    // snprintf with size limit (safer)
    std::snprintf(buffer, sizeof(buffer), "%s%d", "Value: ", x);

    return 0;
}

Formatted Input

Function Description
scanfFormatted input from stdin
fscanfFormatted input from a stream
sscanfFormatted input from a string

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>
#include <memory>

int main() {
    int x{};
    double y{};

    // scanf from stdin
    std::printf("Enter two numbers: ");
    std::scanf("%d%lf", ::std::addressof(x), ::std::addressof(y));

    // fscanf from file (with RAII)
    ::fast_io::c_file cf(u8"data.txt", ::fast_io::open_mode::in);
    std::fscanf(cf.fp, "%d%lf", ::std::addressof(x), ::std::addressof(y));

    // sscanf from string
    char const* str{"42 3.14"};
    std::sscanf(str, "%d%lf", ::std::addressof(x), ::std::addressof(y));

    return 0;
}

Character and Line I/O

Function Description
fgetc / getcRead a character from a stream
fgetsRead a line from a stream
fputc / putcWrite a character to a stream
fputsWrite a string to a stream
putsWrite a string to stdout (adds newline)
ungetcPush a character back onto a stream
getsRead a line from stdin (deprecated, unsafe)

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>

int main() {
    // Write characters (with RAII)
    ::fast_io::c_file cf_out(u8"test.txt", ::fast_io::open_mode::out);
    std::fputc('A', cf_out.fp);
    std::fputs("Hello\n", cf_out.fp);

    // Read characters
    ::fast_io::c_file cf_in(u8"test.txt", ::fast_io::open_mode::in);
    int ch{std::fgetc(cf_in.fp)};
    std::printf("First char: %c\n", ch);

    // Read line
    char buffer[100];
    std::fgets(buffer, sizeof(buffer), cf_in.fp);
    std::printf("Line: %s", buffer);

    // Push back
    std::ungetc(ch, cf_in.fp);

    // puts to stdout
    std::puts("Hello, World!");  // Adds newline automatically

    return 0;
}

Binary I/O

Function Description
freadRead binary data from a stream
fwriteWrite binary data to a stream

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>

struct Point {
    int x;
    int y;
};

int main() {
    Point p1{10, 20};
    Point p2{};

    // Write binary data (with RAII)
    ::fast_io::c_file cf_out(u8"points.bin", ::fast_io::open_mode::out | ::fast_io::open_mode::binary);
    std::fwrite(&p1, sizeof(Point), 1, cf_out.fp);

    // Read binary data
    ::fast_io::c_file cf_in(u8"points.bin", ::fast_io::open_mode::in | ::fast_io::open_mode::binary);
    std::fread(&p2, sizeof(Point), 1, cf_in.fp);

    std::printf("Read point: (%d, %d)\n", p2.x, p2.y);

    return 0;
}

File Positioning

Function Description
fseekSet file position
ftellGet current file position
rewindSet file position to beginning
fgetposGet file position (portable)
fsetposSet file position (portable)

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>

int main() {
    // File positioning (with RAII)
    ::fast_io::c_file cf(u8"test.txt", ::fast_io::open_mode::in | ::fast_io::open_mode::out);
    std::fputs("Hello, World!", cf.fp);

    // Get current position
    long pos{std::ftell(cf.fp)};
    std::printf("Current position: %ld\n", pos);

    // Seek to beginning
    std::rewind(cf.fp);

    // Or use fseek
    std::fseek(cf.fp, 0, SEEK_SET);  // Beginning
    std::fseek(cf.fp, 0, SEEK_END);  // End
    std::fseek(cf.fp, 5, SEEK_SET);  // Position 5

    // Portable positioning
    std::fpos_t position;
    std::fgetpos(cf.fp, &position);
    // ... do something ...
    std::fsetpos(cf.fp, &position);  // Restore position

    return 0;
}

Error Handling

Function Description
feofTest for end-of-file
ferrorTest for error
clearerrClear error and end-of-file indicators
perrorPrint error message to stderr

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>

int main() {
    ::fast_io::c_file cf(u8"test.txt", ::fast_io::open_mode::in);

    int ch;
    while ((ch = std::fgetc(cf.fp)) != EOF) {
        std::putchar(ch);
    }

    if (std::feof(cf.fp)) {
        std::printf("\nEnd of file reached\n");
    }
    if (std::ferror(cf.fp)) {
        std::printf("\nError occurred\n");
    }

    std::clearerr(cf.fp);  // Clear error/EOF flags
    return 0;
}

Warning: fgetc returns int, not char. This is because it must be able to return EOF (typically -1), which is not a valid char value. Always store the result in an int:


// WRONG - char cannot hold EOF
char ch;
while ((ch = std::fgetc(fp)) != EOF)  // Bug!

// CORRECT - int can hold EOF
int ch;
while ((ch = std::fgetc(fp)) != EOF)  // Works

This is a classic C pitfall. In fast_io, scanning functions handle EOF automatically and return error codes, so you don’t need to worry about this issue.

File Management

Function Description
removeDelete a file
renameRename a file
tmpfileCreate a temporary file
tmpnamGenerate a temporary filename (unsafe)

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>

int main() {
    // Rename a file
    std::rename("old.txt", "new.txt");

    // Delete a file
    std::remove("unwanted.txt");

    // Create temporary file using fast_io's io_temp (RAII)
    ::fast_io::c_file tmp(::fast_io::io_temp);
    std::fprintf(tmp.fp, "%s", "Temporary data\n");
    // File is automatically deleted when tmp goes out of scope

    return 0;
}

Buffering Control

Function Description
setvbufSet stream buffering mode
setbufSet stream buffer

#include <fast_io.h>
#include <fast_io_device.h>
#include <cstdio>

int main() {
    ::fast_io::c_file cf(u8"test.txt", ::fast_io::open_mode::out);

    char buffer[1024];

    // Set custom buffer
    std::setvbuf(cf.fp, buffer, _IOFBF, sizeof(buffer));  // Full buffering
    std::setvbuf(cf.fp, nullptr, _IOLBF, 0);  // Line buffering
    std::setvbuf(cf.fp, nullptr, _IONBF, 0);  // No buffering

    // Simpler interface
    std::setbuf(cf.fp, buffer);  // Equivalent to setvbuf(cf.fp, buffer, _IOFBF, BUFSIZ)
    std::setbuf(cf.fp, nullptr);    // Unbuffered

    return 0;
}