let fd = try FileDescriptor.open(<#filepath#>, .readOnly)
I can load the file's contents to an UnsafeMutableRawBufferPointer using fd.read(into:), but is this the correct first step for loading a file's contents into a string? If so,
I need to allocate the UnsafeMutableRawBufferPointer using .allocate(byteCount:alignment:) before I can pass it to fd.read(into:). The correct byteCount depends on the file's size. So how do I get a file's size using Swift System?
How do I get a string from the UnsafeMutableRawBufferPointer?
That works, but String(contentsOf:) is in Foundation.framework, which — as far as I know — has issues with portability and conversion costs (as long as the new, multiplatform Swift Foundation is still under construction). I was just curious as to whether there is a "pure Swift" way of reading a file to a String, which is why I was experimenting with Swift System.
Foundation …has issues with portability and conversion costs
While that’s true, you have to balance those negatives against the time and risk [1] associated with reimplementing basic stuff like this. For something like reading a file, Foundation works just fine on all platforms supported by Swift.
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple
[1] Remember the Swift System requires you to mess around with unsafe constructs and I generally try to avoid unsafe stuff wherever possible.
As for the conversion to string part use String(cString: ...) or the equivalent if the file content is not in ascii or utf-8 encoding. You may need to add the trailing zero for some of those API's (in particularly those that do not take buffer length).