# Why read(io::IOBuffer, String) does not work as expected

**URL:** https://discourse.julialang.org/t/why-read-io-iobuffer-string-does-not-work-as-expected/25754
**Category:** General Usage
**Created:** [June 27, 2019, 8:56am UTC](https://discourse.julialang.org/t/why-read-io-iobuffer-string-does-not-work-as-expected/25754 "2019-06-27T08:56:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)
#### Post date: [June 27, 2019, 8:56am UTC](https://discourse.julialang.org/t/why-read-io-iobuffer-string-does-not-work-as-expected/25754/1 "2019-06-27T08:56:45Z")

</div>

```julia
io = IOBuffer()
write(io, "name, age \n tom, 12")
data = read(io, String) # return empty string, not as expected 

```

The following code works

```julia
data = String( take!(io) ) # return `name, age \n tom, 12` as expected

```

Why the API is designed in this way ? Is it better to make read(io, String) return the string ?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [June 27, 2019, 9:01am UTC](https://discourse.julialang.org/t/why-read-io-iobuffer-string-does-not-work-as-expected/25754/2 "2019-06-27T09:01:38Z")

</div>

`read(io, ...)` will start reading the buffer at its current position, if you want to read from the start you should roll back the buffer with `seekstart`:

```julia
julia> io = IOBuffer();

julia> write(io, "name, age \n tom, 12");

julia> data = read(seekstart(io), String)
"name, age \n tom, 12"

```

---

<div class="post-metadata">

### Author: ![zhangliye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zhangliye/32/3208_2.png) [@zhangliye](https://discourse.julialang.org/u/zhangliye)
#### Post date: [June 27, 2019, 9:13am UTC](https://discourse.julialang.org/t/why-read-io-iobuffer-string-does-not-work-as-expected/25754/3 "2019-06-27T09:13:05Z")

</div>

Thank you so much for your quick response. When call `read` on text file, it can read the whole content as follows.

```julia
data = read(raw"/data/test.txt", String) # returns the whole contents

```

it is easier for the users to keep the API consistent. Maybe it is better to set the position to the start in the function read().

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [June 27, 2019, 9:19am UTC](https://discourse.julialang.org/t/why-read-io-iobuffer-string-does-not-work-as-expected/25754/4 "2019-06-27T09:19:36Z")

</div>

That method is something very different though, it opens a io to the file, with the current position at the start of the file.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 27, 2019, 1:25pm UTC](https://discourse.julialang.org/t/why-read-io-iobuffer-string-does-not-work-as-expected/25754/5 "2019-06-27T13:25:52Z")

</div>

The API is consistent. Your original example is equivalent to

```julia
julia> io = open("test.txt", read=true, write=true, create=true)
IOStream(<file test.txt>)

julia> write(io, "name, age \n tom, 12")
19

julia> data = read(io, String)
""

```

whereas your `read` file example is equivalent to

```julia
julia> read(IOBuffer("name, age \n tom, 12"), String)
"name, age \n tom, 12"

```

You can’t expect `read` on the same io to return the same result no matter what the state of the io is since it’s a stateful API by design. If you are looking for an API that always returns the same content for seekable IO (essentially what you meant by “API consistent”), you can always define a function that always does the seek and then read. It’s just easy enough to do, not generally useful, and definitely not what `read` is for so it certainly won’t become the implementation of `read`.
