[ruby-core:63880] [ruby-trunk - Bug #10039] "a+" mode for File.open doesn’t work

From: nobu@...
Date: 2014-07-19 15:48:36 UTC
List: ruby-core #63880
Issue #10039 has been updated by Nobuyoshi Nakada.

Backport changed from 2.0.0: UNKNOWN, 2.1: UNKNOWN to 2.0.0: REQUIRED, 2.1:=
 REQUIRED

----------------------------------------
Bug #10039: "a+" mode for File.open doesn=E2=80=99t work
https://bugs.ruby-lang.org/issues/10039#change-47904

* Author: Marvin G=C3=BClker
* Status: Closed
* Priority: Normal
* Assignee:=20
* Category: doc
* Target version:=20
* ruby -v: ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
* Backport: 2.0.0: REQUIRED, 2.1: REQUIRED
----------------------------------------
Hi there,

According to [the documentation](http://www.ruby-doc.org/core-2.1.2/IO.html=
#method-c-new) the "a+" file open mode should open a file:

> Read-write, starts at end of file if file exists

However, it appears that the "a+" mode behaves exactly as "r+", i.e. the fi=
le pointer is placed at the beginning of the file instead of the end.

This example demonstrates the problem:

~~~
(288) [20:37:02 quintus@hades] /tmp/foobar
% cat test.txt=20
test
test2
(289) [20:37:07 quintus@hades] /tmp/foobar
% irb
irb(main):001:0> f =3D File.open("test.txt", "a+")
=3D> #<File:test.txt>
irb(main):002:0> f.pos
=3D> 0
irb(main):003:0> f.eof?
=3D> false
irb(main):004:0> f.read
=3D> "test\ntest2\n"
irb(main):005:0> f.pos
=3D> 10
irb(main):006:0> f.close
=3D> nil
irb(main):007:0>=20
~~~

The file position pointer should have been placed at the end of the file (i=
.e. position 10), not at the beginning (which is what "r+" is for).

Vale,
Quintus



--=20
https://bugs.ruby-lang.org/

In This Thread

Prev Next