Stream: beginners

Topic: Reading a file


view this post on Zulip Sebastian Porto (Dec 02 2022 at 01:35):

Hi, can you give me an example on how to read a file from disk?
Can't find anything in the tutorial or the standard library.
Thanks

view this post on Zulip Sebastian Porto (Dec 02 2022 at 01:41):

Found this https://github.com/roc-lang/roc/blob/main/examples/cli/file.roc

view this post on Zulip Brendan Hansknecht (Dec 02 2022 at 01:47):

That is a small app that does a mix of file io

view this post on Zulip Brendan Hansknecht (Dec 02 2022 at 01:47):

https://github.com/roc-lang/roc/blob/main/examples/cli/file.roc#L29

view this post on Zulip Brendan Hansknecht (Dec 02 2022 at 01:47):

is reading a file

view this post on Zulip Brendan Hansknecht (Dec 02 2022 at 01:48):

Docs here: https://www.roc-lang.org/packages/basic-cli/File

view this post on Zulip Brendan Hansknecht (Dec 02 2022 at 01:48):

I'm a bit surprised that isn't linked in the tutorial

view this post on Zulip Sebastian Porto (Dec 02 2022 at 02:35):

Alright, so far I have this

main : Task {} []
main =
    content <- readFile "day01/sample" |> await
    Stdout.line "I read the file back. Its contents: \"\(content)\""

readFile : Str -> Task Str []
readFile = \filePath ->
    task =
        File.readUtf8 (Path.fromStr filePath)
    attempt task \result ->
        when result is
            Err _ -> Task.succeed "Error"
            Ok content -> Task.succeed content

Next step is to see how I can pipe this into somethig else


Last updated: Jul 06 2025 at 12:14 UTC