Project

General

Profile

Actions

Bug #10039

closed

"a+" mode for File.open doesn’t work

Bug #10039: "a+" mode for File.open doesn’t work

Added by Quintus (Marvin Gülker) over 11 years ago. Updated about 11 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
[ruby-core:63747]

Description

Hi there,

According to the documentation 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 file 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 
test
test2
(289) [20:37:07 quintus@hades] /tmp/foobar
% irb
irb(main):001:0> f = File.open("test.txt", "a+")
=> #<File:test.txt>
irb(main):002:0> f.pos
=> 0
irb(main):003:0> f.eof?
=> false
irb(main):004:0> f.read
=> "test\ntest2\n"
irb(main):005:0> f.pos
=> 10
irb(main):006:0> f.close
=> nil
irb(main):007:0> 

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

Updated by jaredbeck (Jared Beck) over 11 years ago Actions #1 [ruby-core:63856]

Writing with a+ mode appears to work fine, but the pos cursor is not updated until the first IO operation.

echo -en "test1\ntest2\n" > test

irb
irb(main):001:0> f = File.open('test', 'a+')
=> #<File:test>
irb(main):002:0> f.pos
=> 0
irb(main):003:0> f.write "test3\n"
=> 6
irb(main):004:0> f.pos
=> 18
irb(main):005:0> f.close
=> nil

cat test
test1
test2
test3

Notice how the pos cursor is updated after the first write.

EDIT: If you trace the system calls (I used dtruss on my mac), you'll see a call to lseek (man 2 lseek) after the first pos, and before the write.

Updated by nobu (Nobuyoshi Nakada) over 11 years ago Actions #2 [ruby-core:63877]

  • Category set to doc

Append mode doesn't move the pointer at open.
It moves it to the end at each write(2).

Updated by nobu (Nobuyoshi Nakada) over 11 years ago Actions #3 [ruby-core:63878]

  • Status changed from Open to Closed
  • % Done changed from 0 to 100

Applied in changeset r46876.


io.c: fix rdoc of append mode

  • io.c (rb_io_initialize): [DOC] fix rdoc of append mode. it does
    not move the pointer at open. [ruby-core:63747] [Bug #10039]

Updated by nobu (Nobuyoshi Nakada) over 11 years ago Actions #4 [ruby-core:63880]

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

Updated by nagachika (Tomoyuki Chikanaga) about 11 years ago Actions #5 [ruby-core:64749]

  • Backport changed from 2.0.0: REQUIRED, 2.1: REQUIRED to 2.0.0: REQUIRED, 2.1: DONE

Backported into ruby_2_1 branch at r47377.

Updated by usa (Usaku NAKAMURA) about 11 years ago Actions #6 [ruby-core:64913]

  • Backport changed from 2.0.0: REQUIRED, 2.1: DONE to 2.0.0: DONE, 2.1: DONE

backported into ruby_2_0_0 at r47498.

Actions

Also available in: PDF Atom