FileDescriptor
public struct FileDescriptor
The FileDescriptor struct is used to perform low-level file operations.
Example: Open and read a ASCII file
import SwiftIO
// Open the file located in "/subDir/hello.txt"
let file = FileDescriptor.open("/SD:/subDir/hello.txt")
//Initialize a buffer to store the reading bytes.
var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 10, alignment: 4)
// Read bytes into the buffer
file.read(into: buffer)
// Print the reding bytes.
for i in 0..<buffer.count {
print(buffer[i])
}
while true {
sleep(ms: 1000)
}
-
Options for specifying what a file descriptor’s offset is relative to.
See moreDeclaration
Swift
public enum SeekOrigin : Int32
-
Open or creates, if does not exist, file.
Attention
The root directory for the micro SD card is “/SD:/”Declaration
Swift
public static func open(_ path: String) -> FileDescriptor
Parameters
path
REQUIRED The location of the file to open.
-
Deletes the specified file or directory
Declaration
Swift
public static func unlink(_ path: String)
Parameters
path
REQUIRED The location of the file to open.
-
Flushes the associated stream and closes the file.
Declaration
Swift
public func close()
-
Get current file position.
Declaration
Swift
public func tell() -> Int
Return Value
Current position in file.
-
Reads bytes at the current file offset into a buffer
Declaration
Swift
public func read(into buffer: UnsafeMutableRawBufferPointer) -> Int
Parameters
buffer
REQUIRED Te region of memory to read into.
-
Reads bytes at the specified offset into a buffer.
Declaration
Swift
public func read(fromAbsoluteOffest offset: Int, into buffer: UnsafeMutableRawBufferPointer) -> Int
Parameters
offset
REQUIRED The file offset where reading begins.
buffer
REQUIRED Te region of memory to read into.
-
Reposition the offset for the given file descriptor.
Declaration
Swift
public func seek(offset: Int, from whence: SeekOrigin = .start) -> Int
Parameters
offset
REQUIRED The new offset for the file descriptor.
whence
OPTIONAL The origin of the new offset.
-
Writes the contents of a buffer at the current file offset.
Declaration
Swift
public func write(_ buffer: UnsafeRawBufferPointer)
Parameters
buffer
REQUIRED The region of memory that contains the data being written.
-
Writes the contents of a string at the current file offset.
Declaration
Swift
public func write(_ string: String)
Parameters
string
REQUIRED The string being written.
-
Writes the contents of a buffer at the specified offset.
Declaration
Swift
public func write(toAbsoluteOffset offset: Int, _ buffer: UnsafeRawBufferPointer)
Parameters
offset
REQUIRED The file offset where writing begins.
buffer
REQUIRED Te region of memory that contains the data being written.
-
Flushes any cached write of an open file.
Attention
This method can be used to flush the cache of an open file. This can be called to ensure data gets written to the storage media immediately. This may be done to avoid data loss if power is removed unexpectedly. Note that closing a file will cause caches to be flushed correctly so it need not be called if the file is being closed.Declaration
Swift
public func sync()