# Simple \`redirect\_stdout\` example

**URL:** <https://discourse.julialang.org/t/simple-redirect-stdout-example/135204>\
**Category:** General Usage\
**Created:** [January 22, 2026, 12:48pm UTC](https://discourse.julialang.org/t/simple-redirect-stdout-example/135204 "2026-01-22T12:48:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattsignorelli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattsignorelli/32/221502_2.png) [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Post date:** [January 22, 2026, 12:48pm UTC](https://discourse.julialang.org/t/simple-redirect-stdout-example/135204/1 "2026-01-22T12:48:29Z")

</div>

I am struggling to get any example with `redirect_stdout` to work. There isn’t any documentation on it. E.g.

```julia
julia> f, io = mktemp();

julia> fout = open(f, "w+");

julia> redirect_stdout(fout) do
       println("Hello!")
       end;

julia> close(fout)

julia> read(fout)
UInt8[]

julia> read(fout, String)
""

```

Any ideas? most of the examples I find on the discourse are too old, and no longer work

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 22, 2026, 1:04pm UTC](https://discourse.julialang.org/t/simple-redirect-stdout-example/135204/2 "2026-01-22T13:04:59Z")

</div>

Don’t read from a closed file handle or the end of a file. After the redirect block, try `seek(fout, 0); read(fout, String)`.

---

<div class="post-metadata">

**Author:** ![mattsignorelli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattsignorelli/32/221502_2.png) [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Post date:** [January 22, 2026, 1:24pm UTC](https://discourse.julialang.org/t/simple-redirect-stdout-example/135204/3 "2026-01-22T13:24:09Z")

</div>

Thanks! This works with Julia I/O, but not when calling C. In fact something strange happens where it dumps the C stdout upon exiting Julia:

```julia-auto
% julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.10.10 (2025-06-27)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> f, ios = mktemp();

julia> fout = open(f, "w+");

julia> redirect_stdout(fout) do
         @ccall printf("hello!"::Cstring)::Cint;
       end
6

julia> seek(fout, 0); read(fout, String)
""

julia> exit()
hello!%   

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 22, 2026, 1:38pm UTC](https://discourse.julialang.org/t/simple-redirect-stdout-example/135204/4 "2026-01-22T13:38:33Z")

</div>

> [@mattsignorelli](#):
>
> Thanks! This works with Julia I/O, but not when calling C. In fact something strange happens where it dumps the C stdout upon exiting Julia:

“Something strange” is probably [buffering](https://sourceware.org/glibc/manual/latest/html_node/Stream-Buffering.html) of the I/O on the C side? Try calling [`Libc.flush_cstdio()`](https://docs.julialang.org/en/v1/base/libc/#Base.Libc.flush_cstdio) after the `printf`.

---

<div class="post-metadata">

**Author:** ![mattsignorelli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mattsignorelli/32/221502_2.png) [@mattsignorelli](https://discourse.julialang.org/u/mattsignorelli)\
**Post date:** [January 22, 2026, 1:51pm UTC](https://discourse.julialang.org/t/simple-redirect-stdout-example/135204/5 "2026-01-22T13:51:28Z")

</div>

That worked! Thank you!

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [January 22, 2026, 2:02pm UTC](https://discourse.julialang.org/t/simple-redirect-stdout-example/135204/6 "2026-01-22T14:02:45Z")

</div>

Might have to `flush` on the Julia side too, I’ve been taking the apparent timeliness of REPL printing for granted but writes can be delayed by buffering otherwise.
