Categories
Tags
3d algorithms alignment analyze APIT Arc Architecture arm ascii assembly asynchronous base64 BitHacks Blogging box c c23 clang clang-format client clippy cmake compiler Computer concat concurrency const_fn constexpr contravariant cos covariant cpp cpu crate CS Customization cybersecurity DataStructure db debugging Demo deserialization discrete doc DP drawio dtruss Dynamic emulator example Example FFI flamegraph flat_map fold format FP fsanitize Functional FunctionalProgramming functions futures Fuwari game GATs gcc gccrs generics gitignore glibc GUI hacking hashmap haskell heap hyperfine Imperative interop invariant iterator join justfile kernel LaTeX leak LFU linux lto MachineLearning macOS map Markdown math ML mmap mod nc OnceLock optimization OS ownership panic parallels perf physics pin postgresql product profiling pub radare2 rayon release reverse RPIT rust sanitizer Science science serialization server shift sin size SmallProjects socket std strace String StringView strip strlen struct sum super surrealdb SWAR swisstable synchronous tan thread time toml tracing traits triangulation uint32_t UnsafeRust utf16 utf8 Video vulkan wsl x86_64 xilem zig
354 words
2 minutes
glibc_stdio_h
link
- This is glibc’s
stdio.hheader - the standard C I/O library interface. Let me explain the main function categories and their purposes:
1. File Operations
| Function | Purpose |
|---|---|
fopen() | Opens a file and returns a FILE stream pointer |
freopen() | Reopens a file with a different mode or reassigns an existing stream |
fclose() | Closes a stream and flushes buffers |
remove() | Deletes a file |
rename() | Moves/renames a file |
fdopen() | Creates a FILE stream from an existing file descriptor |
2. Character I/O
| Function | Purpose |
|---|---|
fgetc() / getc() | Reads one character from a stream |
fgets() | Reads a line (up to newline) into a string buffer |
getchar() | Reads one character from stdin |
fputc() / putc() | Writes one character to a stream |
fputs() | Writes a string to a stream |
putchar() | Writes one character to stdout |
ungetc() | Pushes a character back into the input buffer |
3. Formatted I/O - Output (printf family)
| Function | Purpose |
|---|---|
printf() | Writes formatted output to stdout |
fprintf() | Writes formatted output to a stream |
sprintf() | Writes formatted output to a string buffer (unsafe - no bounds) |
snprintf() | Writes formatted output with size limit (safe) |
asprintf() / vasprintf() | Allocates buffer automatically via malloc |
dprintf() | Writes formatted output to a file descriptor |
4. Formatted I/O - Input (scanf family)
| Function | Purpose |
|---|---|
scanf() | Reads formatted input from stdin |
fscanf() | Reads formatted input from a stream |
sscanf() | Reads formatted input from a string |
5. Block I/O
| Function | Purpose |
|---|---|
fread() | Reads an array of elements (binary data) |
fwrite() | Writes an array of elements (binary data) |
6. File Positioning
| Function | Purpose |
|---|---|
fseek() | Sets file position (uses long) |
fseeko() | Sets file position (uses off_t for large files) |
ftell() / ftello() | Gets current file position |
rewind() | Resets position to start of file |
fgetpos() / fsetpos() | Saves/restores file position (handles large files) |
7. Error Handling
| Function | Purpose |
|---|---|
feof() | Checks if EOF was reached |
ferror() | Checks if an error occurred |
clearerr() | Clears error and EOF flags |
perror() | Prints error message describing current errno |
8. Buffering Control
| Function | Purpose |
|---|---|
setbuf() | Sets buffer (or makes unbuffered if NULL) |
setvbuf() | Sets buffering mode and size (_IOFBF, _IOLBF, _IONBF) |
9. Temporary Files
| Function | Purpose |
|---|---|
tmpfile() | Creates a temporary file (deleted on close) |
tmpnam() | Generates a unique temporary filename |
tempnam() | Generates temp filename with directory/prefix control |
10. Stream Locking (POSIX threads)
| Function | Purpose |
|---|---|
flockfile() | Acquires exclusive lock on a stream |
ftrylockfile() | Tries to acquire lock without blocking |
funlockfile() | Releases lock on a stream |
11. Line-Oriented Input
| Function | Purpose |
|---|---|
getline() | Reads an entire line (allocates/reallocs buffer automatically) |
getdelim() | Reads up to a specified delimiter |
12. Process Pipes
| Function | Purpose |
|---|---|
popen() | Creates a pipe to/from a child process |
pclose() | Closes a pipe and waits for child process |
Key Constants
| Constant | Value/Description |
|---|---|
EOF | End-of-file indicator (-1) |
BUFSIZ | Default buffer size (8192) |
FILENAME_MAX | Maximum filename length |
FOPEN_MAX | Maximum open streams (16) |
SEEK_SET/CUR/END | fseek origins |
_IOFBF/_IOLBF/_IONBF | Buffering modes |
Important Features
- Large File Support:
fopen64,fseeko64,off64_tfor files >2GB - Thread Safety:
_unlockedvariants avoid locking overhead in single-threaded code - Cancellation Points: Many functions can be cancelled by POSIX threads
- GNU Extensions:
renameat2,open_memstream,obstack_printf