Mojo struct
FileDescriptor
@register_passable(trivial)
struct FileDescriptor
File descriptor of a file.
Fields
- value (
Int): The underlying value of the file descriptor.
Implemented traits
AnyType,
Copyable,
ImplicitlyCopyable,
ImplicitlyDestructible,
Movable,
Writer
comptime members
__copyinit__is_trivial
comptime __copyinit__is_trivial = True
__del__is_trivial
comptime __del__is_trivial = True
__moveinit__is_trivial
comptime __moveinit__is_trivial = True
Methods
__init__
__init__(value: Int = 1) -> Self
Constructs the file descriptor from an integer.
Args:
- value (
Int): The file identifier (Default 1 = stdout).
__init__(f: FileHandle) -> Self
Constructs the file descriptor from a file handle.
Args:
- f (
FileHandle): The file handle.
write_bytes
write_bytes(mut self, bytes: Span[Byte, origin])
Write a span of bytes to the file.
Args:
- bytes (
Span): The byte span to write to this file.
read_bytes
read_bytes(mut self, buffer: Span[Byte, origin]) -> UInt
Read a number of bytes from the file into a buffer.
Notes: Reference.
Args:
- buffer (
Span): ASpan[Byte]to read bytes into. Read up tolen(buffer)number of bytes.
Returns:
UInt: Actual number of bytes read.
Raises:
If the operation fails.
isatty
isatty(self) -> Bool
Checks whether a file descriptor refers to a terminal.
Returns True if the file descriptor is open and connected to a
tty(-like) device, otherwise False. On GPUs, the function always
returns False.
Examples:
# Check if stdout is a terminal
if stdout.isatty():
print("Running in a terminal")
else:
print("Output is redirected")Returns:
Bool: True if the file descriptor is connected to a terminal, False otherwise.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!