[swift-users] Simple text file I/O with Swift 3

Ken Burgett kenb at iotone.io
Sat May 28 13:05:26 CDT 2016


Hi all,

I am trying to understand how Swift 3 File I/O works in a linux 
environment.  I put together a trivial test program using what I
can glean from the few examples I can find.  See below.

===============================
import Glibc
import Foundation

let filename = Process.arguments[1]
let file_handle = fopen (filename, "r")
let BUFSIZE = 1024
var buf     = [CChar](repeating:CChar(0), count:BUFSIZE)
while fgets(&buf, Int32(BUFSIZE), file_handle) != nil
{
   print(buf)
}
exit(0)
===============================

The program runs, but produces a stream of integer arrays, each of 1024 
bytes.  I can see what appears to be ASCII character values in the 
sample text, plus a lot of trailing zeros.  I suspect I haven't mapped 
the bytes in buf to Strings, so how should that be done?
-- 
Ken Burgett
Principal Software Engineer
Email: kenb at iotone.io
Office: 530.693.4449
Mobile: 831.332.6846
URL: www.iotone.co
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: main.swift
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160528/82302076/attachment.ksh>


More information about the swift-users mailing list