# What is the benefit of using file handle to write data to stdout?

**URL:** <https://forums.kodeco.com/t/what-is-the-benefit-of-using-file-handle-to-write-data-to-stdout/78474>\
**Category:** Swift\
**Created:** [August 7, 2019, 12:37pm UTC](https://forums.kodeco.com/t/what-is-the-benefit-of-using-file-handle-to-write-data-to-stdout/78474 "2019-08-07T12:37:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryanlee](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ryanlee/32/81494_2.png) [@ryanlee](https://forums.kodeco.com/u/ryanlee)\
**Post date:** [August 7, 2019, 12:37pm UTC](https://forums.kodeco.com/t/what-is-the-benefit-of-using-file-handle-to-write-data-to-stdout/78474/1 "2019-08-07T12:37:33Z")

</div>

I just curious about the swift code template: e.g. [Birthday Cake Candles | HackerRank](https://www.hackerrank.com/challenges/birthday-cake-candles/problem)

```swift
import Foundation

// Complete the birthdayCakeCandles function below.
func birthdayCakeCandles(ar: [Int]) -> Int {

}

let stdout = ProcessInfo.processInfo.environment["OUTPUT_PATH"]!
FileManager.default.createFile(atPath: stdout, contents: nil, attributes: nil)
let fileHandle = FileHandle(forWritingAtPath: stdout)!

guard let arCount = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!)
else { fatalError("Bad input") }

guard let arTemp = readLine() else { fatalError("Bad input") }
let ar: [Int] = arTemp.split(separator: " ").map {
    if let arItem = Int($0.trimmingCharacters(in: .whitespacesAndNewlines)) {
        return arItem
    } else { fatalError("Bad input") }
}

guard ar.count == arCount else { fatalError("Bad input") }

let result = birthdayCakeCandles(ar: ar)

fileHandle.write(String(result).data(using: .utf8)!)
fileHandle.write("\n".data(using: .utf8)!)

```

I can make the code work with just

```swift
print(result)

```

and without any code to create `FileHandle` to the stdout, I just can’t see the point to make those code overcomplicated.

* * *

off topic:  
how to pass the environment variables when I run the code from command line?  
I am failed to make it work using the following command:

```auto
OUTPUT_PATH=`pwd` && xcrun swift abc.swift

```

---

<div class="post-metadata">

**Author:** ![robertomachorro](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/robertomachorro/32/111603_2.png) [@robertomachorro](https://forums.kodeco.com/u/robertomachorro)\
**Post date:** [August 8, 2019, 1:05am UTC](https://forums.kodeco.com/t/what-is-the-benefit-of-using-file-handle-to-write-data-to-stdout/78474/2 "2019-08-08T01:05:07Z")

</div>

Hi @ryanlee and welcome to the community!

I’m answering your off topic, [ProcessInfo](https://developer.apple.com/documentation/foundation/processinfo) in Foundation is your friend for anything related to the system processes. You generally work with its singleton: _processInfo_.

In particular, you are looking for the [environment](https://developer.apple.com/documentation/foundation/processinfo/1417911-environment) array:

if let dbsecret = ProcessInfo.processInfo.environment[“dbpassword”] {  
// your code here  
}

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [January 21, 2020, 4:37am UTC](https://forums.kodeco.com/t/what-is-the-benefit-of-using-file-handle-to-write-data-to-stdout/78474/3 "2020-01-21T04:37:32Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
