simple i/o on linux with open source swift


(K Richard Pixley) #1

I'm a new swift person, /long/ time unix/linux developer, mac user but
only mac developer inasmuch as mac is unix. I've installed swift on my
ubuntu box, written hello world, etc.

Now... to write much of anything else, I need a way to
open/read/write/close a file. I don't see anything like that in the
swift standard library, (aside from print()). And I don't see anything
in the way of documentation for the Core Library.

How do I do i/o in swift?

I'm not thinking in terms of "an app", just a simple, read from stdin,
write to stdout filter. How?

Pointers welcome.

--rich


(Jacob Bandes-Storch) #2

If I've surmised correctly from other messages on this thread, you should
be able to `import Glibc` and then you can use most of what's in the C
standard library (since Swift interoperates directly with C). The module
map which enables you to import Glibc is:
https://github.com/apple/swift/blob/master/stdlib/public/Platform/glibc.modulemap.gyb

OO wrappers around things like I/O are provided by the open source version
of Foundation: https://github.com/apple/swift-corelibs-foundation and, to
the extent that their functionality matches the proprietary macOS version,
there's documentation at https://developer.apple.com/reference/foundation/

Jacob

···

On Sat, Jul 16, 2016 at 4:33 PM, K Richard Pixley via swift-users < swift-users@swift.org> wrote:

I'm a new swift person, *long* time unix/linux developer, mac user but
only mac developer inasmuch as mac is unix. I've installed swift on my
ubuntu box, written hello world, etc.

Now... to write much of anything else, I need a way to
open/read/write/close a file. I don't see anything like that in the swift
standard library, (aside from print()). And I don't see anything in the
way of documentation for the Core Library.

How do I do i/o in swift?

I'm not thinking in terms of "an app", just a simple, read from stdin,
write to stdout filter. How?

Pointers welcome.

--rich

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Andrey Fidrya) #3

High level ones:

let s = try String(contentsOfFile: filename, encoding: .utf8)
try s.write(toFile: filename, atomically: true, encoding: .utf8)

Low level ones:

import Foundation

let fileManager = FileManager.default()

guard let enumerator = fileManager.enumerator(atPath: path) else { fatalError() }
if !fileManager.fileExists(atPath: filename) {
    fileManager.createFile(atPath: filename, contents: nil, attributes: nil)
}
guard let outFile = FileHandle(forWritingAtPath: filename) else { fatalError() }
defer {
    outFile.closeFile()
}

outFile.truncateFile(atOffset: 0)
guard let data = str.data(using: .utf8) else { fatalError() }
outFile.write(data)

This used to work on Linux.

Andrey

···

On 17 Jul 2016, at 02:33, K Richard Pixley via swift-users <swift-users@swift.org> wrote:

I'm a new swift person, long time unix/linux developer, mac user but only mac developer inasmuch as mac is unix. I've installed swift on my ubuntu box, written hello world, etc.

Now... to write much of anything else, I need a way to open/read/write/close a file. I don't see anything like that in the swift standard library, (aside from print()). And I don't see anything in the way of documentation for the Core Library.

How do I do i/o in swift?

I'm not thinking in terms of "an app", just a simple, read from stdin, write to stdout filter. How?

Pointers welcome.

--rich
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users