stdout, stderr, and variable argument lists

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

stdout, stderr, and variable argument lists

Perry E. Metzger
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 [hidden email]

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20151123205313.7f137c49%40jabberwock.cb.piermont.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: stdout, stderr, and variable argument lists

Daniel T.
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"...
 
The answer to (2) above is here: http://www.drivenbycode.com/the-missing-apply-function-in-swift/
 

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/d695fedd-7091-4b06-9b70-cb977dddcdeb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: stdout, stderr, and variable argument lists

Adam Sharp
In reply to this post by Perry E. Metzger
On 24 Nov 2015, at 12:53 PM, Perry E. Metzger <[hidden email]> 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

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/20A5ADC8-4666-440F-90F3-7464627C1DC9%40me.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: stdout, stderr, and variable argument lists

Vinicius Vendramini
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))
Credits to this guy on Stack Exchange: http://stackoverflow.com/questions/24041554/how-can-i-output-to-stderr-with-swift


On Wednesday, November 25, 2015 at 7:26:07 PM UTC-5, Adam Sharp wrote:
On 24 Nov 2015, at 12:53 PM, Perry E. Metzger <<a href="javascript:" target="_blank" gdf-obfuscated-mailto="2nmppIMgCAAJ" rel="nofollow" onmousedown="this.href=&#39;javascript:&#39;;return true;" onclick="this.href=&#39;javascript:&#39;;return true;">pe...@...> 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

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/09a402a7-125d-48c5-bd13-f90aa5694dbe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.