Simple text file I/O with Swift 3 (Quinn "The Eskimo!")

(Ken Burgett) #1

Hi Quinn,

The link you referenced above <> is all about Swift 2.2. I had already found that one on StackOverflow and gave it a try, which caused me to generate the string of queries, when it all fell apart under Swift 3.


On 2016-05-30 10:00, wrote:

Send swift-users mailing list submissions to

To subscribe or unsubscribe via the World Wide Web, visit
or, via email, send a message with subject or body 'help' to

You can reach the person managing the list at

When replying, please edit your Subject line so it is more specific
than "Re: Contents of swift-users digest..."

Today's Topics:

   1. Re: Simple text file I/O with Swift 3 (Quinn "The Eskimo!")


Message: 1
Date: Mon, 30 May 2016 09:11:41 +0100
From: "Quinn \"The Eskimo!\"" <>
To: Swift Users List <>
Subject: Re: [swift-users] Simple text file I/O with Swift 3
Message-ID: <>
Content-Type: text/plain; charset=us-ascii

On 28 May 2016, at 19:05, Ken Burgett via swift-users > <> wrote:


The trick here is to replace the above line with:

    print(String(validatingUTF8: buf))

`fgets` sets up `buf` to hold a C string, so you have to convert it to
a Swift string. How do you do this depends on the encoding of the
bytes. If you expect the C string to be UTF-8, then
`String(validatingUTF8:)` is the way to go.

IMPORTANT: This conversion can fail, which is why the above will print
a bunch of optional strings, and you will have to decide what to your
program should do when it does.

                   * * *

btw This question came up recently. See the thread for other
suggestions about how to handle it.


Share and Enjoy
Quinn "The Eskimo!" <>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


swift-users mailing list

End of swift-users Digest, Vol 6, Issue 28

Ken Burgett
Principal Software Engineer
Office: 530.693.4449
Mobile: 831.332.6846