File, Dir, IO - wow I struggle
-
I have no clue what my problem is, but I just don't get IO. I grew up on DOS, so navigating a folder structure with commands is not confusing to me. But somehow doing it in Ruby is like a living nightmare. Please help debug this simple code:
index_file_path = File.join( Dir.pwd, 'selected_index.xml') UI.messagebox index_file_path f = File.new(index_file_path, "r+")
As best I can tell, that is putting the file right where I want it - the messagebox is showing the exact path I am expecting due to a few previous lines of code that set the active directory to my working directory.
But when I run it, I get:
No such file or directory - C:/Users/Fullmer.Family/Desktop/Dropbox/plugin/temp_model/selected_index.xml
Which of course it doesn't exist, that is why I'm trying to creat it with
File.new
Anyhow, obviously I'm just being incredibly dense here and probably supplying an incorrect object type somewhere or something. So let me have it, what am I doing wrong. Thanks,
Chris
-
Check your file access flag: http://ruby-doc.org/core-1.8.6/index.html
You're using
r+
@unknownuser said:
Read-write, starts at beginning of file.
But you want to be using
w+
@unknownuser said:
Read-write, truncates existing file to zero length or creates a new file for reading and writing.
-
file=File.open(file_path, opt)
where 'opt' is:
"r"
= reads from an existing file [default]
"r+"
= reads from or writes to an existing file [default]
"w"
= writes to a file [it makes the file if it doesn't exist]
"w+"
= reads from or writes to a file [it makes the file if it doesn't exist]
"a"
= appends to a file [it makes the file if it doesn't exist]
"a+"
= reads from or appends to a file [it makes the file if it doesn't exist]
Thenfile.puts("Hello world!")
.
Remember to usefile.close
when you are done.
For binary data you can use 'binmode' - which can be useful when 'copying' files...
from=File.open(from_path, "r") from.binmode data=from.read from.close ###manipulate 'data' maybe? to=File.open(to_path, "w") to.binmode to.write(data) to.close
-
I prefer to use the block variant to ensure the file always closes itself - even on errors.
<span class="syntaxdefault"><br />File</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">open</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">Ā file</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">Ā </span><span class="syntaxstring">'w+'</span><span class="syntaxdefault">Ā </span><span class="syntaxkeyword">)</span><span class="syntaxdefault">Ā </span><span class="syntaxkeyword">{</span><span class="syntaxdefault">Ā </span><span class="syntaxkeyword">|</span><span class="syntaxdefault">file</span><span class="syntaxkeyword">|<br /></span><span class="syntaxdefault">Ā Ā putsĀ </span><span class="syntaxstring">'HelloĀ world'<br /></span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">Ā </span>
-
Good tip to ensure that it IS closed...
Also you ought to check that the file is open for writing too - e.g. if you are writing to an existing file that you might have open in another application, so it is locked; or your file_path might be invalid, or it might have insufficient permissions etc...begin file=File.open(file_path, "w") rescue ### trap if already open UI.messagebox("XXX File;\n\n "+file_path+"\n\nCannot be written - it's probably already open.\nClose it and try again...\n\nExiting...") return nil ### OR add some other tidy-up code etc... end ### as we've not exited use 'file'... ### OT use tt's {||} block in the begin part
-
D'oh! Thanks Thom, you nailed it on the head. Use w+ to start a new file. What does it mean when it says it will truncate the length to zero or start a new file? Is it saying it will erase everything in the file if there is data already?
I've already got myself setup using the block to write info into my file, thanks for that reminder. I need to make sure I get in and out of files cleanly.
Thanks for the examples TIG. I'm sure I'll need to be checking if the file exists or is open already.
Thanks guys,
Chris
-
The option 'r' reads, and 'r+' reads and writes, - it wipes existing data: the file must exist already: using the 'write' part of 'r+' is like using the 'write' part 'w+', BUT it will not make the file if it doesn't exist.
The option 'w' writes, and 'w+' write and reads, - it wipes any existing data in the file - use this for whenever you want just the one set of data - like a 'report' that is updated: if the file doesn't exist it's made for you.
The option 'a' appends, and 'a+' appends and reads - the data is 'appended' to [added on to the end of] any data that is already in the file, i.e. it keeps the current contents - like a 'log' file that keeps a list that grows over time: if the file doesn't exist it's made for you. -
Ahh, interesting. Odd to me that a and w both make new files, but r does not. Oh well. Very good, now to fill my files with useful data. Thanks guys,
Chris
Advertisement