# About writing file?

**URL:** <https://www.ruby-forum.com/t/about-writing-file/137434>\
**Category:** Ruby\
**Created:** [May 13, 2008, 5:58am UTC](https://www.ruby-forum.com/t/about-writing-file/137434 "2008-05-13T05:58:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [May 13, 2008, 5:58am UTC](https://www.ruby-forum.com/t/about-writing-file/137434/1 "2008-05-13T05:58:39Z")

</div>

hi, I have problem about writing file.  
I use this

data = File.new("/options.sav",“w”)

and I get file name option.sav in my path but when I open it with  
notepad. It can modify :’( I don’t want this because options.sav has  
save data from my app that can’t hack it! how can I write file with  
can’t be modified???

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [May 13, 2008, 6:27am UTC](https://www.ruby-forum.com/t/about-writing-file/137434/2 "2008-05-13T06:27:11Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Pat K. wrote:  
| hi, I have problem about writing file.  
| I use this  
|  
| data = File.new(“/options.sav”,“w”)  
|  
| and I get file name option.sav in my path but when I open it with  
| notepad. It can modify 😢 I don’t want this because options.sav has  
| save data from my app that can’t hack it! how can I write file with  
| can’t be modified???

You can’t. All it needs is the correct privileges to access the file.

If you want the data to be somewhat tamper-proof, use encryption. Note,  
though, that all it needs is the key to tamper with the file again. Or  
you could obfuscate the file (ROT13, BASE64 encoding, what have you).  
It’ll be a loosing battle, though.

If you find a solution to this, the RIAA and MPAA, as well as SecuROM  
would like to have a word with you, since you made DRM possible. 😉

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ “Some things don’t need the thought people give them.” -Hobbes  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgpGHgACgkQbtAgaoJTgL8lUgCeKxh9TO2SdeOu+Wnlj1yCgN7g  
kmEAnizWOaVe6fw6oqLsKe3XgVvUx8xF  
=2h6b  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [May 13, 2008, 11:20am UTC](https://www.ruby-forum.com/t/about-writing-file/137434/3 "2008-05-13T11:20:43Z")

</div>

If you just want to prevent casual/accidental, rather than  
determined modification, after you close the file:

File.chmod("/options.sav", 0440);

For example.

RF

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [May 13, 2008, 1:20pm UTC](https://www.ruby-forum.com/t/about-writing-file/137434/4 "2008-05-13T13:20:26Z")

</div>

Ron F. wrote:

> If you just want to prevent casual/accidental, rather than  
> determined modification, after you close the file:
> 
> File.chmod("/options.sav", 0440);

Does 0440 work on windows too?

(I think it’s clear, when he meantions “notepad”, and saves to the root  
folder, that he’s using Windows.)

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [May 14, 2008, 11:11am UTC](https://www.ruby-forum.com/t/about-writing-file/137434/5 "2008-05-14T11:11:05Z")

</div>

Yes, but I did get the arguments backwards.

File.chmod(0440, “/options.sav”)

Works just fine on my windows box and does set the Readonly bit when  
I look at the file later. The nice thing about good high level  
languages, they abstract many differences in operating systems away  
making it easier to write portable code.

Ron.

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [May 14, 2008, 11:25am UTC](https://www.ruby-forum.com/t/about-writing-file/137434/6 "2008-05-14T11:25:38Z")

</div>

thx you I will try with it 🙂

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [May 20, 2008, 9:50am UTC](https://www.ruby-forum.com/t/about-writing-file/137434/7 "2008-05-20T09:50:41Z")

</div>

It’s not work! :’(  
I use that and it make read-only file that I can’t modify it but when I  
right click-\>property and check off in read-only box. it can modify!!

☹

---

<div class="post-metadata">

**Author:** ![blitzer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ecae2f/32.png) [@blitzer](https://www.ruby-forum.com/u/blitzer)\
**Post date:** [May 20, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/about-writing-file/137434/8 "2008-05-20T14:19:40Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Pat K. wrote:  
| It’s not work! 😢  
| I use that and it make read-only file that I can’t modify it but when I  
| right click-\>property and check off in read-only box. it can modify!!

And you will not be able to change that behavior, short of shipping your  
own operating system.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

You thought I was taking your woman away from you. You’re jealous.  
You tried to kill me with your bare hands. Would a Kelvan do that?  
Would he have to? You’re reacting with the emotions of a human.  
You are human.  
~ – Kirk, “By Any Other Name,” stardate 4657.5  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgywckACgkQbtAgaoJTgL+AEwCeI2xF3bQE8FjtyOlpQ5ekib9n  
2JAAnRMK9fHw+IRnx1N7aeOjBeeSTe4x  
=OTa6  
-----END PGP SIGNATURE-----
