Module

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 __stdio

Functions

function fclose(stream: *mut void): i32

C fclose; closes a stream.

function fdopen(fd: i32, mode: *u8): *mut void

C fdopen; wraps an existing file descriptor as a stream.

function feof(stream: *mut void): i32

C feof; tests the stream EOF indicator.

function ferror(stream: *mut void): i32

C ferror; tests the stream error indicator.

function fflush(stream: *mut void): i32

C fflush; flushes stream buffers.

function fgets(s: *mut u8, size: i32, stream: *mut void): *mut u8

C fgets; reads a line or byte limit from a stream.

function fopen(pathname: *u8, mode: *u8): *mut void

C fopen; opens a stream by path and mode.

function fputs(s: *u8, stream: *mut void): i32

C fputs; writes a NUL-terminated string to a stream.

function fread(ptr: *mut void, size: u64, nmemb: u64, stream: *mut void): u64

C fread; reads array elements from a stream.

function fseek(stream: *mut void, offset: i64, whence: i32): i32

C fseek; repositions a stream.

function ftell(stream: *mut void): i64

C ftell; returns the current stream position.

function fwrite(ptr: *void, size: u64, nmemb: u64, stream: *mut void): u64

C fwrite; writes array elements to a stream.

function mkstemp(template: *mut u8): i32

POSIX 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): i32

C remove; removes a filesystem entry.

function rename(oldpath: *u8, newpath: *u8): i32

C rename; renames a filesystem entry.

function rewind(stream: *mut void): void

C rewind; returns a stream to the beginning.

Constants

const EOF: i32
const SEEK_CUR: i32
const SEEK_END: i32
const SEEK_SET: i32
namespace Stdio

C standard I/O streams (stdio.h)

Functions

function fclose(stream: *mut void): i32

Closes stream. Returns 0 on success, EOF on error.

function fdopen(fd: i32, mode: *u8): *mut void

Associates a stream with an existing file descriptor fd.

The mode must be compatible with the fd’s open mode.

function feof(stream: *mut void): i32

Returns non-zero if the end-of-file indicator is set on stream.

function ferror(stream: *mut void): i32

Returns non-zero if the error indicator is set on stream.

function fflush(stream: *mut void): i32

Flushes 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 u8

Reads 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 void

Opens 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): i32

Writes 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): u64

Reads 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): i32

Sets 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): i64

Returns the current position of stream, or -1 on error.

function fwrite(ptr: *void, size: u64, nmemb: u64, stream: *mut void): u64

Writes nmemb elements of size bytes each from ptr to stream.

Returns the number of elements successfully written.

function mkstemp(template: *mut u8): i32

Creates 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): i32

Deletes the file at pathname. Returns 0 on success.

function rename(oldpath: *u8, newpath: *u8): i32

Renames oldpath to newpath. Returns 0 on success.

function rewind(stream: *mut void): void

Resets stream to the beginning and clears error/EOF indicators.

Constants

const EOF: i32
const SEEK_CUR: i32
const SEEK_END: i32
const SEEK_SET: i32