# Some confusion around how IO works for an IOStream opened in append mode

**URL:** <https://discourse.julialang.org/t/some-confusion-around-how-io-works-for-an-iostream-opened-in-append-mode/8148>\
**Category:** General Usage\
**Created:** [January 4, 2018, 4:41am UTC](https://discourse.julialang.org/t/some-confusion-around-how-io-works-for-an-iostream-opened-in-append-mode/8148 "2018-01-04T04:41:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [January 4, 2018, 4:41am UTC](https://discourse.julialang.org/t/some-confusion-around-how-io-works-for-an-iostream-opened-in-append-mode/8148/1 "2018-01-04T04:41:18Z")

</div>

I don’t know very much about IO, but I’m struggling a little bit with something and was just wondering if the behaviour is as expected. I create a binary file with one `Float64` in it as follows:

`open(fid->write(fid, 1.0), "/home/colin/aaa.dat", "w")`

Now I re-open the file in append mode:

`fid = open("/home/colin/aaa.dat", "a")`

We have `position(fid)` evaluates to `8`, which makes sense, since we should open at the end of the file if we want to append. Also, `eof(fid)` evaluates to `true`. However, if I do the following:

`seekstart(fid)`

then now `position(fid)` evaluates to `0` as expected. But `eof(fid)` still evaluates to `true`!?! And further, if I try `read(fid, Float64)`, I get an end-of-file error.

In other words, it seems that if I open the file in append mode, then any call to `seek` doesn’t actually get me away from the end of the file, even though the `position` call seems to indicate that I’ve moved.

Is this behaviour expected?

Cheers,

Colin

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [January 4, 2018, 5:02am UTC](https://discourse.julialang.org/t/some-confusion-around-how-io-works-for-an-iostream-opened-in-append-mode/8148/2 "2018-01-04T05:02:44Z")

</div>

Note, I can see the logic in not allowing the user to write anything to any location other than the end of the file. But my understanding that reading arbitrary locations from the file should still be possible in append mode…

---

<div class="post-metadata">

**Author:** ![bicycle1885](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bicycle1885/32/107_2.png) [@bicycle1885](https://discourse.julialang.org/u/bicycle1885)\
**Post date:** [January 4, 2018, 5:04am UTC](https://discourse.julialang.org/t/some-confusion-around-how-io-works-for-an-iostream-opened-in-append-mode/8148/3 "2018-01-04T05:04:46Z")

</div>

Though the docstring of `open` does not exhaustively describe the behavior of append mode, it looks quite natural to me. The append mode is designed for append data to a file, so its position should always be at the end of a file, and you cannot read data from it.

From [fopen(3) - Linux manual page](http://man7.org/linux/man-pages/man3/fopen.3.html) :

> Opening a file in append mode (a as the first character of mode) causes all subsequent write operations to this stream to occur at end-of-file, as if preceded the call: `fseek(stream, 0, SEEK_END);`

If you’d like to read/write data from/to arbitrary position of a file, I think you should open the file with “r+” mode.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [January 4, 2018, 5:22am UTC](https://discourse.julialang.org/t/some-confusion-around-how-io-works-for-an-iostream-opened-in-append-mode/8148/4 "2018-01-04T05:22:49Z")

</div>

Nice link. That makes sense. It sounds like I could also use “a+” if I wanted to read from arbitrary positions, but only wanted to be able to write to the end of the file.

I guess you could maybe construct an argument that any `seek` call should result in an error, rather than making it seem like the position in the file has changed, even though you are really still at the end of the file, but this feels pretty low priority, as well as subjective.

Thanks for responding.

Cheers,

Colin
