<!--
{
  "availability" : [
    "iOS: 14.0.0 -",
    "iPadOS: 14.0.0 -",
    "macCatalyst: 14.0.0 -",
    "macOS: 11.0.0 -",
    "tvOS: 14.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 7.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "System",
  "identifier" : "/documentation/System/FileDescriptor",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "System"
    ],
    "preciseIdentifier" : "s:6System14FileDescriptorV"
  },
  "title" : "FileDescriptor"
}
-->

# FileDescriptor

An abstract handle to an input or output data resource,
such as a file or a socket.

```
@frozen struct FileDescriptor
```

## Overview

You are responsible for managing the lifetime and validity
of `FileDescriptor` values,
in the same way as you manage a raw C file handle.

## Topics

### Creating a File Descriptor

[`init(rawValue: CInt)`](/documentation/System/FileDescriptor/init(rawValue:))

Creates a strongly-typed file handle from a raw C file handle.

[`let rawValue: CInt`](/documentation/System/FileDescriptor/rawValue)

The raw C file handle.

### Opening a File

[`open(_:_:options:permissions:retryOnInterrupt:)`](/documentation/System/FileDescriptor/open(_:_:options:permissions:retryOnInterrupt:)-4ql4b)

[`open(_:_:options:permissions:retryOnInterrupt:)`](/documentation/System/FileDescriptor/open(_:_:options:permissions:retryOnInterrupt:)-5t3xn)

[`struct AccessMode`](/documentation/System/FileDescriptor/AccessMode)

The desired read and write access for a newly opened file.

[`struct OpenOptions`](/documentation/System/FileDescriptor/OpenOptions)

Options that specify behavior for a newly-opened file.

### Reading From a File

[`func read(into: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool) throws -> Int`](/documentation/System/FileDescriptor/read(into:retryOnInterrupt:))

Reads bytes at the current file offset into a buffer.

[`func read(fromAbsoluteOffset: Int64, into: UnsafeMutableRawBufferPointer, retryOnInterrupt: Bool) throws -> Int`](/documentation/System/FileDescriptor/read(fromAbsoluteOffset:into:retryOnInterrupt:))

Reads bytes at the specified offset into a buffer.

### Changing a File’s Offset

[`func seek(offset: Int64, from: FileDescriptor.SeekOrigin) throws -> Int64`](/documentation/System/FileDescriptor/seek(offset:from:))

Repositions the offset for the given file descriptor.

[`struct SeekOrigin`](/documentation/System/FileDescriptor/SeekOrigin)

Options for specifying what a file descriptor’s offset is relative to.

### Writing To A File

[`func write(UnsafeRawBufferPointer, retryOnInterrupt: Bool) throws -> Int`](/documentation/System/FileDescriptor/write(_:retryOnInterrupt:))

Writes the contents of a buffer at the current file offset.

[`func write(toAbsoluteOffset: Int64, UnsafeRawBufferPointer, retryOnInterrupt: Bool) throws -> Int`](/documentation/System/FileDescriptor/write(toAbsoluteOffset:_:retryOnInterrupt:))

Writes the contents of a buffer at the specified offset.

[`func writeAll<S>(S) throws -> Int`](/documentation/System/FileDescriptor/writeAll(_:))

Writes a sequence of bytes to the current offset
and then updates the offset.

[`func writeAll<S>(toAbsoluteOffset: Int64, S) throws -> Int`](/documentation/System/FileDescriptor/writeAll(toAbsoluteOffset:_:))

Writes a sequence of bytes to the given offset.

### Closing a File

[`func close() throws`](/documentation/System/FileDescriptor/close())

Deletes a file descriptor.

[`func closeAfter<R>(() throws -> R) throws -> R`](/documentation/System/FileDescriptor/closeAfter(_:))

Runs a closure and then closes the file descriptor, even if an error occurs.

### Encoding File Descriptors

### Comparing File Descriptors

### Structures

[`struct DuplicateOptions`](/documentation/System/FileDescriptor/DuplicateOptions)

Options that specify behavior for a duplicated file descriptor.

[`struct PipeOptions`](/documentation/System/FileDescriptor/PipeOptions)

Options that specify behavior for a newly-created pipe.

### Instance Methods

[`func duplicate(as: FileDescriptor, options: FileDescriptor.DuplicateOptions, retryOnInterrupt: Bool) throws(Errno) -> FileDescriptor`](/documentation/System/FileDescriptor/duplicate(as:options:retryOnInterrupt:))

Duplicates this file descriptor and returns the newly created copy.

[`func duplicate(as: FileDescriptor?, retryOnInterrupt: Bool) throws -> FileDescriptor`](/documentation/System/FileDescriptor/duplicate(as:retryOnInterrupt:))

Duplicates this file descriptor and returns the newly created copy.

[`func resize(to: Int64, retryOnInterrupt: Bool) throws`](/documentation/System/FileDescriptor/resize(to:retryOnInterrupt:))

Truncates or extends the file referenced by this file descriptor.

[`func stat(retryOnInterrupt: Bool) throws(Errno) -> Stat`](/documentation/System/FileDescriptor/stat(retryOnInterrupt:))

Creates a `Stat` struct for the file referenced by this `FileDescriptor`.

### Type Properties

[`static var standardError: FileDescriptor`](/documentation/System/FileDescriptor/standardError)

The standard error file descriptor, with a numeric value of 2.

[`static var standardInput: FileDescriptor`](/documentation/System/FileDescriptor/standardInput)

The standard input file descriptor, with a numeric value of 0.

[`static var standardOutput: FileDescriptor`](/documentation/System/FileDescriptor/standardOutput)

The standard output file descriptor, with a numeric value of 1.

### Type Methods

[`static func pipe() throws -> (readEnd: FileDescriptor, writeEnd: FileDescriptor)`](/documentation/System/FileDescriptor/pipe())

Creates a unidirectional data channel, which can be used for interprocess communication.

[`static func pipe(options: FileDescriptor.PipeOptions) throws(Errno) -> (readEnd: FileDescriptor, writeEnd: FileDescriptor)`](/documentation/System/FileDescriptor/pipe(options:))

Creates a unidirectional data channel, which can be used for interprocess communication.

## Relationships

### Conforms To

[`Decodable`](/documentation/Swift/Decodable)

[`Equatable`](/documentation/Swift/Equatable)

[`Encodable`](/documentation/Swift/Encodable)

[`BitwiseCopyable`](/documentation/Swift/BitwiseCopyable)

[`Sendable`](/documentation/Swift/Sendable)

[`Hashable`](/documentation/Swift/Hashable)

[`RawRepresentable`](/documentation/Swift/RawRepresentable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Copyable`](/documentation/Swift/Copyable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)