Page 1 of 1

some sort of basic text file operations

Posted: Mon Sep 30, 2013 11:49 pm
by tester
Quick question. How to change content of a text file? I know how to add something, but... how have no idea how to remove :-) (without additional windows dialogs)

Something like this. Load the text file, edit content and save the file (not "save as").

Re: some sort of basic text file operations

Posted: Tue Oct 01, 2013 12:23 am
by RJHollins
Hi Tester,

You probably know about this already, but there was an SM thread that took on a 'Text Editor'.

Here's the thread:
http://synthmaker.co.uk/forum/viewtopic.php?f=12&t=10588&st=0&sk=t&sd=a&hilit=text+editor

Not sure how the project ended up ... but maybe a start. And with RUBY possibilities, maybe a better design could be realized ?!?
8-)

Re: some sort of basic text file operations

Posted: Tue Oct 01, 2013 12:41 am
by billv
tester wrote:Something like this. Load the text file, edit content and save the file (not "save as").

Load and save.fsm
(4.49 KiB) Downloaded 996 times

Re: some sort of basic text file operations

Posted: Tue Oct 01, 2013 12:59 am
by tester
Thanks billiv.

RJ - this project is not music related, nor CPU heavy (green only), so can be fully with ruby.

Re: some sort of basic text file operations

Posted: Tue Oct 01, 2013 4:02 am
by billv
tester wrote:can be fully with ruby

If you come up with ruby load/save..please post....
I'm not getting very far using stuff online...

Code: Select all

def event i,v
File.open("C:\Notes\Data2.txt","r+")
end

gets

Code: Select all

Errno: {in method 'event')::ENOENT:No such file or directory-C:NotesData2.txt 


I agree Ruby: I don't recognize that file or directory without the backslashes either....
So I'm still not writing the method right... have tried declaring things but still missing something..

Re: some sort of basic text file operations

Posted: Tue Oct 01, 2013 9:39 am
by trogluddite
billv wrote:I agree Ruby: I don't recognize that file or directory without the backslashes either...

:lol:
That's one you have to watch out for in Ruby - there's a difference between "double-quoted" and 'single-quoted' strings when the code gets parsed by Ruby.
In double-quoted strings, the backslash gets read as an escape character - so that you can write things like "\n" for new-line, and "\t" for tab. "\N" and "\D" don't represent anything special, so the escapes just get ignored in your example.
Inside double's you can also write things like...
"The value of my variable is #{@my_variable}"
...to insert calculated values into strings (the #{ represents the start and } the end of the substitution). Even whole expressions will work like that...
"Ten plus twelve equals #{10+12}"

Single quoted strings are always taken literally with no escapes or substitutions - so it's best to use single quotes around filenames/paths written into the code.
(NB - single quoted are also slightly faster to process because there are no special characters for Ruby to look out for.)

Your little code also contains a cardinal sin of file operations - assuming the file is found, you have opened it, but haven't closed it. That can lead to all sorts of problems, as access will then be denied to any other process/app' that needs to use the file. Closing also ensures that Windows clears any buffered data and writes it to the file.

Assuming it's just strings that you want to load and save, the easiest way is...

To read a string...
File.read('path') ...or... File.read('path', start_position, length) - to read only part of a file.
That will return a String. Or you might prefer File.readlines('path') - which gives you a String array containing each line in turn.
Both of these methods sort out the opening and closing for you, so there's no need to worry about accidentally leaving files open.

To write a string...
A bit more tricky as you do need to explicitly open the file...

Code: Select all

my_file = File.open('path', 'w')
my_file.write(my_string)
my_file.close

You can put as many sequential writes between the open and close as you like.
There's also a slightly more compact version that uses a 'do...end' code block...

Code: Select all

File.open('path', 'w') do |my_file|   # All of this bit must be on one line of code
  my_file.write(my_string)
end

The do...end version is usually preferred because it also closes the file for you after the 'end', so you're less likely to end up with an accidentally open file due to a code typo'.
You'll see in both of them that you have to store the reference to the file in a local variable (e.g. 'my_file')

If you didn't see the 'read' and 'write' methods when you looked through the Ruby API - don't worry, your not going mad. 'File' is a sub-class of the 'IO' class of objects that can handle all sorts of other input/output operations - so most methods from the 'IO' class will also work for files.

Re: some sort of basic text file operations

Posted: Tue Oct 01, 2013 10:54 am
by billv
Thanks Trog...makes more sense now..
will have another go at it...