std::libc::stdio
LibC Stdio
Buffered stream I/O from stdio.h, exposed as LibC::Stdio.
These operate on opaque FILE* streams (CType::FilePtr), in contrast
to the raw file-descriptor operations in LibC::File (unistd.h).
Constants
| Constant | Description |
|---|---|
SEEK_SET |
Seek from beginning of file |
SEEK_CUR |
Seek from current position |
SEEK_END |
Seek from end of file |
EOF |
End-of-file / error indicator (-1) |
Functions
| Function | Description |
|---|---|
fopen |
Open file by pathname, returns stream |
fdopen |
Associate stream with existing file descriptor |
fclose |
Close stream |
fread |
Read binary data from stream |
fwrite |
Write binary data to stream |
fgets |
Read line (up to newline or EOF) |
fputs |
Write null-terminated string to stream |
fseek |
Set stream position |
ftell |
Get current stream position |
rewind |
Reset stream to beginning |
fflush |
Flush buffered output |
feof |
Test end-of-file indicator |
ferror |
Test error indicator |
remove |
Delete file by pathname |
rename |
Rename file |
mkstemp |
Create and open a unique temporary file |
Example
import LibC, CType from "std::libc";
function main(): i32 {
let fp: CType::FilePtr = LibC::Stdio::fopen("log.txt", "w");
if fp == null {
return 1;
}
LibC::Stdio::fputs("line one\n", fp);
LibC::Stdio::fputs("line two\n", fp);
LibC::Stdio::fflush(fp);
// Seek back to the beginning and overwrite
LibC::Stdio::fseek(fp, 0, LibC::Stdio::SEEK_SET);
LibC::Stdio::fputs("REPLACED\n", fp);
LibC::Stdio::fclose(fp);
return 0;
}
namespace __stdioFunctions
function fclose(stream: *mut void): i32C fclose; closes a stream.
function fdopen(fd: i32, mode: *u8): *mut voidC fdopen; wraps an existing file descriptor as a stream.
function feof(stream: *mut void): i32C feof; tests the stream EOF indicator.
function ferror(stream: *mut void): i32C ferror; tests the stream error indicator.
function fflush(stream: *mut void): i32C fflush; flushes stream buffers.
function fgets(s: *mut u8, size: i32, stream: *mut void): *mut u8C fgets; reads a line or byte limit from a stream.
function fopen(pathname: *u8, mode: *u8): *mut voidC fopen; opens a stream by path and mode.
function fputs(s: *u8, stream: *mut void): i32C fputs; writes a NUL-terminated string to a stream.
function fread(ptr: *mut void, size: u64, nmemb: u64, stream: *mut void): u64C fread; reads array elements from a stream.
function fseek(stream: *mut void, offset: i64, whence: i32): i32C fseek; repositions a stream.
function ftell(stream: *mut void): i64C ftell; returns the current stream position.
function fwrite(ptr: *void, size: u64, nmemb: u64, stream: *mut void): u64C fwrite; writes array elements to a stream.
function mkstemp(template: *mut u8): i32POSIX mkstemp; creates and opens a unique temporary file in place.
template must be mutable and end in six X characters. The host replaces
those bytes with the created path and returns an open file descriptor.
function remove(pathname: *u8): i32C remove; removes a filesystem entry.
function rename(oldpath: *u8, newpath: *u8): i32C rename; renames a filesystem entry.
function rewind(stream: *mut void): voidC rewind; returns a stream to the beginning.
Constants
const EOF: i32const SEEK_CUR: i32const SEEK_END: i32const SEEK_SET: i32namespace StdioC standard I/O streams (stdio.h)
Functions
function fclose(stream: *mut void): i32Closes stream. Returns 0 on success, EOF on error.
function fdopen(fd: i32, mode: *u8): *mut voidAssociates a stream with an existing file descriptor fd.
The mode must be compatible with the fd’s open mode.
function feof(stream: *mut void): i32Returns non-zero if the end-of-file indicator is set on stream.
function ferror(stream: *mut void): i32Returns non-zero if the error indicator is set on stream.
function fflush(stream: *mut void): i32Flushes buffered output to stream. Pass null to flush all open streams.
Returns 0 on success, EOF on error.
function fgets(s: *mut u8, size: i32, stream: *mut void): *mut u8Reads at most size - 1 characters from stream into s, stopping at
newline or EOF. The buffer is null-terminated.
Returns s on success, or null on error/EOF.
function fopen(pathname: *u8, mode: *u8): *mut voidOpens the file at pathname with the given mode (“r”, “w”, “a”, etc.).
Returns a FilePtr, or null on failure (check LibC::Errno::get()).
function fputs(s: *u8, stream: *mut void): i32Writes the string s to stream (without trailing newline).
Returns a non-negative value on success, or EOF on error.
function fread(ptr: *mut void, size: u64, nmemb: u64, stream: *mut void): u64Reads up to nmemb elements of size bytes each from stream into ptr.
Returns the number of elements successfully read.
function fseek(stream: *mut void, offset: i64, whence: i32): i32Sets the position of stream. whence is SEEK_SET, SEEK_CUR, or SEEK_END.
Returns 0 on success, -1 on error.
function ftell(stream: *mut void): i64Returns the current position of stream, or -1 on error.
function fwrite(ptr: *void, size: u64, nmemb: u64, stream: *mut void): u64Writes nmemb elements of size bytes each from ptr to stream.
Returns the number of elements successfully written.
function mkstemp(template: *mut u8): i32Creates a unique temporary file from template and returns its fd.
template must end with six X characters and is mutated in place to the
created path. The returned descriptor is open for reading and writing and
should be closed by the caller. Returns -1 on error.
function remove(pathname: *u8): i32Deletes the file at pathname. Returns 0 on success.
function rename(oldpath: *u8, newpath: *u8): i32Renames oldpath to newpath. Returns 0 on success.
function rewind(stream: *mut void): voidResets stream to the beginning and clears error/EOF indicators.
Constants
const EOF: i32const SEEK_CUR: i32const SEEK_END: i32const SEEK_SET: i32