Ch11.11.1: Complete C I/O Function Reference
File Opening and Closing
| Function |
Description |
fopen | Open a file |
fclose | Close a file |
fflush | Flush 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 |
printf | Formatted output to stdout |
fprintf | Formatted output to a stream |
sprintf | Formatted output to a string buffer |
snprintf | Formatted output to a string buffer with size limit |
vprintf | Formatted output to stdout (variadic) |
vfprintf | Formatted output to a stream (variadic) |
vsprintf | Formatted output to a string buffer (variadic) |
vsnprintf | Formatted 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 |
scanf | Formatted input from stdin |
fscanf | Formatted input from a stream |
sscanf | Formatted 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 / getc | Read a character from a stream |
fgets | Read a line from a stream |
fputc / putc | Write a character to a stream |
fputs | Write a string to a stream |
puts | Write a string to stdout (adds newline) |
ungetc | Push a character back onto a stream |
gets | Read 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 |
fread | Read binary data from a stream |
fwrite | Write 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 |
fseek | Set file position |
ftell | Get current file position |
rewind | Set file position to beginning |
fgetpos | Get file position (portable) |
fsetpos | Set 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 |
feof | Test for end-of-file |
ferror | Test for error |
clearerr | Clear error and end-of-file indicators |
perror | Print 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 |
remove | Delete a file |
rename | Rename a file |
tmpfile | Create a temporary file |
tmpnam | Generate 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 |
setvbuf | Set stream buffering mode |
setbuf | Set 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;
}