stdout, stderr, and variable argument lists

21 views
Skip to first unread message

Perry E. Metzger

unread,
Nov 23, 2015, 8:53:16 PM11/23/15
to swift-l...@googlegroups.com
Sorry to be asking so many basic questions. :(

1) Are there any names in the standard library for standard output and
standard error? (I'd ask about stdin but there doesn't seem to be any
standard way of reading from anything other than readLine?)

1.5) For that matter, is there any way to do things like opening and
closing files without calling in to the Objective C Foundation?

2) Is there any way to pass an array generated from a variable
argument list to a function that takes a variable argument list? I
think the answer to this is "no"...

Perry
--
Perry E. Metzger pe...@piermont.com

Daniel T.

unread,
Nov 23, 2015, 9:34:57 PM11/23/15
to Swift Language
On Monday, November 23, 2015 at 8:53:16 PM UTC-5, Perry Metzger wrote:
Sorry to be asking so many basic questions. :(

1) Are there any names in the standard library for standard output and
standard error? (I'd ask about stdin but there doesn't seem to be any
standard way of reading from anything other than readLine?)

1.5) For that matter, is there any way to do things like opening and
closing files without calling in to the Objective C Foundation?

I think the answers to the above are, "No, you have to use Foundation classes/methods."
 
2) Is there any way to pass an array generated from a variable
argument list to a function that takes a variable argument list? I
think the answer to this is "no"...
 

Adam Sharp

unread,
Nov 25, 2015, 7:26:07 PM11/25/15
to Perry E. Metzger
On 24 Nov 2015, at 12:53 PM, Perry E. Metzger <pe...@piermont.com> wrote:

> Sorry to be asking so many basic questions. :(

Please, don't feel bad for asking questions! You should feel absolutely welcome to. :)

> 1) Are there any names in the standard library for standard output and
> standard error? (I'd ask about stdin but there doesn't seem to be any
> standard way of reading from anything other than readLine?)
>
> 1.5) For that matter, is there any way to do things like opening and
> closing files without calling in to the Objective C Foundation?

The Darwin module is an interface to the C standard library, and exports `stdin`, `stderr` and `stdout` as `UnsafeMutablePointer<FILE>` (a.k.a. `FILE *`):

% swift
Welcome to Apple Swift version 2.1 (700.1.101.6 700.1.76). Type :help for assistance.
1> import Darwin
2> stdin
$R0: UnsafeMutablePointer<FILE> = {
_rawValue = 0x00007fff77c0c2b0 __sF
}
3> stdout
$R1: UnsafeMutablePointer<FILE> = {
_rawValue = 0x00007fff77c0c348 __sF + 152
}
4> stderr
$R2: UnsafeMutablePointer<FILE> = {
_rawValue = 0x00007fff77c0c3e0 __sF + 304
}

But there's no standard way, insofar as there's nothing in the standard library. The options as I understand them are to use Foundation, or standard C functions.

–Adam

Vinicius Vendramini

unread,
Nov 25, 2015, 7:35:11 PM11/25/15
to Swift Language, pe...@piermont.com
As an alternative to directly using the C stuff (but still unfortunately outside the swift standard library) there's an NSFileHandle class that has a fileHandleWithStandardError, fileHandleWithStandardInput and fileHandleWithStandardOutput methods. It might be a better (i.e. higher-level) way of doing what you want:

import Foundation

// Create a file handle to work with
let stderr = NSFileHandle.fileHandleWithStandardError()

// Build up a string; whatever you want
let stuff = "something"
let something = "I'm a string with \(stuff) in it\n"

// Write it
stderr.writeData(something.dataUsingEncoding(NSUTF8StringEncoding))
Reply all
Reply to author
Forward
0 new messages