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,
Movable,
UnknownDestructibility,
Writer
Aliases
__copyinit__is_trivial
alias __copyinit__is_trivial = True
__del__is_trivial
alias __del__is_trivial = True
__moveinit__is_trivial
alias __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[UInt8, 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[UInt8, 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.
write
write[*Ts: Writable](mut self, *args: *Ts)
Write a sequence of Writable arguments to the provided Writer.
Parameters:
- *Ts (
Writable): Types of the provided argument sequence.
Args:
- *args (
*Ts): Sequence of arguments to write to this Writer.
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.
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!