IO
Undocumented
-
Undocumented
-
Undocumented
-
Synonym for IO.new.
See also
IO.newDeclaration
Swift
public class func forfd(_ fd: Int, mode: String) -> IO
Parameters
fd
A file descriptor.
mode
A string mode.
Return Value
An IO stream.
-
Returns a new IO object (a stream) for the given integer file descriptor fd and mode string. opt may be used to specify parts of mode in a more readable fashion.
Declaration
Swift
public class func open(_ fd: Int, mode: String = "r") -> IO
Parameters
fd
A file descriptor.
mode
A string mode.
Return Value
An IO stream.
-
Reassociates I/O stream with the I/O stream given in another I/O or to a new stream opened on path. This may dynamically change the actual class of this stream.
Declaration
Swift
public func reopen(_ io: IO) -> IO
Parameters
io
Another I/O stream.
Return Value
Self with an new file pointer.
-
Reassociates I/O stream with the I/O stream given in another I/O or to a new stream opened on path. This may dynamically change the actual class of this stream.
Declaration
Swift
public func reopen(_ path: String, _ mode: String = "r") -> IO
Parameters
path
A file path.
mode
A mode string.
Return Value
Self with an new file pointer.
-
With no associated block,
IO.open
is a synonym forIO.new
. If the optional code block is given, it will be passed io as an argument, and the IO object will automatically be closed when the block terminates.Declaration
Swift
public class func new(_ fd: Int, mode: String = "r", closure: ((Void) -> ())? = nil) -> IO
Parameters
fd
A file descriptor.
mode
A string mode.
Return Value
An IO stream.
-
Reads length bytes from the I/O stream. Length must be a non-negative integer or
File#read
will return empty string.let file = "file.txt" File.read(file) #=> "RbSwift\n" File.read(file, offset: 1) #=> "bSwift\n" File.read(file, offset: 100) #=> ""
Declaration
Swift
public class func read(_ name: String, length: Int = Int.max, offset: Int = 0) -> String
Parameters
name
The name of a file.
length
The length of bytes from I/O stream.
offset
The starting position of the I/O Stream.
Return Value
A string read from the I/O Stream.
-
Opens the file with
File#open
and writes the given string to I/O stream. The stream must be opened for writing.File.open("empty.txt", "w") { file in file.write("Content") } let readFile = File.open("empty.txt", "r") readFile.read() #=> "Content"
Declaration
Swift
public class func write(_ name: String, _ string: String, offset: Int = 0, mode: String = "w") -> Int
Parameters
name
The name of a file.
string
A string to write to the specific file.
offset
The starting position of the I/O Stream.
mode
A string for
File#open
Return Value
The number of bytes written.
-
Writes the given object(s) to I/O stream.
File.open("print-empty.txt", "w") { file in file.print("This is ", 10, " lines\n") } let file = File.open("print-empty.txt") file.read() #=> "This is 10 lines\n"
Declaration
Swift
public func print(_ values: CustomStringConvertible...)
Parameters
values
An array of values conforms to
CustomStringConvertible
protocol. -
Writes the given object(s) to I/O stream. Writes a newline after any that do not already end with a newline sequence.
File.open("puts-empty.txt", "w") { file in file.puts("This", "is", "a", "test") } let file = File.open("puts-empty.txt") file.read() #=> "This\nis\na\ntest\n"
See also
An alias toIO#print(values:)
Declaration
Swift
public func puts(_ values: CustomStringConvertible...)
Parameters
values
An array of values conforms to
CustomStringConvertible
protocol. -
If obj is Numeric, write the character whose code is the least-significant byte of obj, otherwise write the first byte of the string representation of obj to I/O stream.
File.open("putc.txt", "w") { file in file.putc("aaa") file.putc(97) } let file = File.open("putc.txt") file.read() #=> "aa"
Declaration
Swift
public func putc(_ char: String)
Parameters
char
A character or a string, if char is a string,
IO#putc(char:)
only write the first char of string into file. -
If obj is Numeric, write the character whose code is the least-significant byte of obj, otherwise write the first byte of the string representation of obj to I/O stream.
File.open("putc.txt", "w") { file in file.putc("aaa") file.putc(97) } let file = File.open("putc.txt") file.read() #=> "aa"
Declaration
Swift
public func putc(_ char: Int)
Parameters
char
A integer which would convert to String by
Int#chr
. -
Reads length bytes from the I/O stream. Length must be a non-negative integer or
File#read
will return empty string.let file = File.open("file.txt") file.read(file) #=> "RbSwift\n" file.read(file) #=> "bSwift\n" file.read(file) #=> ""
Declaration
Swift
public func read(_ length: Int = Int.max) -> String
Parameters
length
The length of bytes from I/O stream.
Return Value
A string read from the I/O Stream.
-
Writes the given string to I/O stream. The stream must be opened for writing.
File.open("empty.txt", "w") { file in file.write("Content") #=> 7 }
Declaration
Swift
public func write(_ string: String) -> Int
Parameters
string
A string written to I/O Stream.
Return Value
The number of bytes written.
-
Reads the next “line” from the I/O stream; lines are separated by sep. A separator of nil and a zero-length separator reads the entire contents.
let file = File.open("file.txt") file.gets() #=> "first line\n" file.gets("s") #=> "s" file.gets() #=> "econd line\n"
Declaration
Swift
public func gets(_ sep: String? = "\n") -> String?
Parameters
sep
A string separator.
Return Value
A string value or nil.
-
Reads a one-character string from IO stream. Returns nil if called at end of file.
let file = Fixture.name("file.txt") File.open(file) { file in file.getc! #=> "f" } let file = Fixture.name("file.txt") File.open(file) { file in file.gets() file.gets() file.getc #=> nil }
Declaration
Swift
public var getc: String?
-
Reads a one-character string from IO stream. Returns nil if called at end of file. An alias to
IO#getc
.Declaration
Swift
public var readchar: String?
-
Reads the next “line” from the I/O stream; lines are separated by sep. A separator of nil and a zero-length separator reads the entire contents.
See also
An alias toIO#gets
.Declaration
Swift
public func readline(_ sep: String? = "\n") -> String?
Parameters
sep
A string separator.
Return Value
A string value or nil.
-
Flushes any buffered data within IO stream to the underlying operating system.
Declaration
Swift
public func flush() -> Int
Return Value
An int return value for
fflush
. -
An enum value as the mapping of
SEEK_SET/SEEK_CUR/SEEK_END
- set: SEEK_SEK
- cur: SEEK_CUR
- end: SEEK_END
- int: A custom seek position for
IO#seek(offset:whence:)
Declaration
Swift
public enum SeekPosition
-
Positions IO stream to the beginning of input, resetting lineno to zero.
file.rewind() <=> file.seek(0)
Declaration
Swift
public func rewind()
-
Seeks to a given
offset
in the stream according to the value ofwhence
.See also
SeekPositionDeclaration
Swift
public func seek(_ offset: Int, whence: SeekPosition = .set)
Parameters
offset
An integer offset.
whence
An whenece
-
Closes I/O stream and flushes any pending writes to the operating system.
Declaration
Swift
public func close() -> Int
Return Value
A status.
-
Returns an integer representing the numeric file descriptor for I/O stream.
Declaration
Swift
public var fileno: Int
-
An alias to
fileno
var.Declaration
Swift
public var to_i: Int
-
Returns true if I/O stream is associated with a terminal device (tty), false otherwise.
Declaration
Swift
public var isatty: Bool
-
The current offset (in bytes) of IO stream. Set this var to seek to the given position (in bytes) in IO stream. It is not guaranteed that seeking to the right position when IO stream is textmode.
Declaration
Swift
public var pos: Int
-
Undocumented