# Writing to a file without opening it

**URL:** <https://www.ruby-forum.com/t/writing-to-a-file-without-opening-it/62568>\
**Category:** Ruby\
**Created:** [June 6, 2006, 6:24am UTC](https://www.ruby-forum.com/t/writing-to-a-file-without-opening-it/62568 "2006-06-06T06:24:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sy\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sy\_Ali](https://www.ruby-forum.com/u/Sy_Ali)\
**Post date:** [June 6, 2006, 6:24am UTC](https://www.ruby-forum.com/t/writing-to-a-file-without-opening-it/62568/1 "2006-06-06T06:24:33Z")

</div>

File.open(file, ‘a+’) won’t work when the file is not readable.

However, in a shell I can append a string using  
echo string \>\> file

Am I forced to use exec + echo to append content, or is there a way to  
append a string to a file using ruby code… without opening the file?

---

<div class="post-metadata">

**Author:** ![Sy\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sy\_Ali](https://www.ruby-forum.com/u/Sy_Ali)\
**Post date:** [June 6, 2006, 6:42am UTC](https://www.ruby-forum.com/t/writing-to-a-file-without-opening-it/62568/2 "2006-06-06T06:42:51Z")

</div>

On Tue, 6 Jun 2006, Sy Ali wrote:

> File.open(file, ‘a+’) won’t work when the file is not readable.
> 
> However, in a shell I can append a string using  
> echo string \>\> file
> 
> Am I forced to use exec + echo to append content, or is there a way to  
> append a string to a file using ruby code… without opening the file?

harp:~ \> rm file

harp:~ \> touch file

harp:~ \> chmod 200 file

harp:~ \> strace sh -c ‘echo string \>\> file’ 2\>&1|grep open|tail -1  
open(“file”, O\_WRONLY|O\_APPEND|O\_CREAT|O\_LARGEFILE, 0666) = 3

harp:~ \> ruby -e’ open(“file”,  
File::WRONLY|File::APPEND|File::CREAT, 0666){|f| f.puts “string2”} ’

harp:~ \> chmod 600 file

harp:~ \> cat file  
string  
string2

-a

---

<div class="post-metadata">

**Author:** ![Sy\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sy\_Ali](https://www.ruby-forum.com/u/Sy_Ali)\
**Post date:** [June 6, 2006, 1:19pm UTC](https://www.ruby-forum.com/t/writing-to-a-file-without-opening-it/62568/3 "2006-06-06T13:19:08Z")

</div>

On 6/6/06, Yoann G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Use mode ‘a’ instead of ‘a+’ in File.open

That worked like a charm, thanks!

---

<div class="post-metadata">

**Author:** ![Sy\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sy\_Ali](https://www.ruby-forum.com/u/Sy_Ali)\
**Post date:** [June 6, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/writing-to-a-file-without-opening-it/62568/4 "2006-06-06T12:26:55Z")

</div>

> File.open(file, ‘a+’) won’t work when the file is not readable.
> 
> However, in a shell I can append a string using  
> echo string \>\> file
> 
> Am I forced to use exec + echo to append content, or is there a way to  
> append a string to a file using ruby code… without opening the file?

Use mode ‘a’ instead of ‘a+’ in File.open

Yoann

---

<div class="post-metadata">

**Author:** ![Sy\_Ali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/54ee81/32.png) [@Sy\_Ali](https://www.ruby-forum.com/u/Sy_Ali)\
**Post date:** [June 7, 2006, 6:11am UTC](https://www.ruby-forum.com/t/writing-to-a-file-without-opening-it/62568/5 "2006-06-07T06:11:13Z")

</div>

On 6/5/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> harp:~ \> ruby -e’ open(“file”, File::WRONLY|File::APPEND|File::CREAT, 0666){|f| f.puts “string2”} ’

Just a quick thanks… this pointed me in the right direction for some  
of the more advanced uses of ‘open’. It’ll come in handy. =)
