# In Swift System, how does one read a file's contents to a string?

**URL:** <https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294>\
**Category:** Using Swift\
**Tags:** swift-system\
**Created:** [June 1, 2023, 3:00am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294 "2023-06-01T03:00:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucca-mito](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucca-mito/32/24744_2.png) [@Lucca-mito](https://forums.swift.org/u/Lucca-mito)\
**Post date:** [June 1, 2023, 3:00am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/1 "2023-06-01T03:00:17Z")

</div>

I have a `FileDescriptor` for a file:

```swift
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,

1. 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?
2. How do I get a string from the `UnsafeMutableRawBufferPointer`?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [June 1, 2023, 3:41am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/2 "2023-06-01T03:41:00Z")

</div>

Isn't `String(contentsOf: url)` good for you?

---

<div class="post-metadata">

**Author:** ![Lucca-mito](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lucca-mito/32/24744_2.png) [@Lucca-mito](https://forums.swift.org/u/Lucca-mito)\
**Post date:** [June 1, 2023, 3:59am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/3 "2023-06-01T03:59:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ibex](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ibex/32/30439_2.png) [@ibex](https://forums.swift.org/u/ibex)\
**Post date:** [June 1, 2023, 5:34am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/4 "2023-06-01T05:34:12Z")

</div>

> [@Lucca-mito](#):
>
> So how do I get a file's size using Swift System?

Start with the [FileManager.FileAttributeKey](https://developer.apple.com/documentation/foundation/fileattributekey/1416548-size).

---

<div class="post-metadata">

**Author:** ![nikolai.ruhe](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nikolai.ruhe/32/23230_2.png) [@nikolai.ruhe](https://forums.swift.org/u/nikolai.ruhe)\
**Post date:** [June 1, 2023, 6:26am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/5 "2023-06-01T06:26:14Z")

</div>

> [@Lucca-mito](#):
>
> So how do I get a file's size using Swift System?

```swift
let size = fd.seek(0, .end) // get file size
fd.seek(0, .start) // return to start of file

```

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [June 1, 2023, 7:59am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/6 "2023-06-01T07:59:51Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![Diggory](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/diggory/32/4320_2.png) [@Diggory](https://forums.swift.org/u/Diggory)\
**Post date:** [June 1, 2023, 8:00am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/7 "2023-06-01T08:00:33Z")

</div>

FileManager is part of Foundation.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [June 1, 2023, 8:03am UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/8 "2023-06-01T08:03:35Z")

</div>

> [@eskimo](#):
>
> [1] Remember the Swift System requires you to mess around with unsafe constructs and I generally try to avoid unsafe stuff wherever possible.

Oh? I thought the premise of Swift System was to expose system APIs while avoiding unsafe constructs.

EDIT: Ah - perhaps you're referring to the fact that [`FileDescriptor.read(...)`](https://developer.apple.com/documentation/system/filedescriptor/read(into:retryoninterrupt:)) reads in to a provided unsafe buffer?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [June 1, 2023, 12:43pm UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/9 "2023-06-01T12:43:37Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [June 1, 2023, 1:44pm UTC](https://forums.swift.org/t/in-swift-system-how-does-one-read-a-files-contents-to-a-string/65294/10 "2023-06-01T13:44:43Z")

</div>

try this:

> <https://github.com/tayloraswift/swift-unidoc/blob/master/Sources/System/FileDescriptor.swift>
