Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

some sort of basic text file operations

For general discussion related FlowStone

some sort of basic text file operations

Postby tester » Mon Sep 30, 2013 11:49 pm

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").
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: some sort of basic text file operations

Postby RJHollins » Tue Oct 01, 2013 12:23 am

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-)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: some sort of basic text file operations

Postby billv » Tue Oct 01, 2013 12:41 am

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 907 times
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia

Re: some sort of basic text file operations

Postby tester » Tue Oct 01, 2013 12:59 am

Thanks billiv.

RJ - this project is not music related, nor CPU heavy (green only), so can be fully with ruby.
Need to take a break? I have something right for you.
Feel free to donate. Thank you for your contribution.
tester
 
Posts: 1786
Joined: Wed Jan 18, 2012 10:52 pm
Location: Poland, internet

Re: some sort of basic text file operations

Postby billv » Tue Oct 01, 2013 4:02 am

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..
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia

Re: some sort of basic text file operations

Postby trogluddite » Tue Oct 01, 2013 9:39 am

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.
All schematics/modules I post are free for all to use - but a credit is always polite!
Don't stagnate, mutate to create!
User avatar
trogluddite
 
Posts: 1730
Joined: Fri Oct 22, 2010 12:46 am
Location: Yorkshire, UK

Re: some sort of basic text file operations

Postby billv » Tue Oct 01, 2013 10:54 am

Thanks Trog...makes more sense now..
will have another go at it...
billv
 
Posts: 1157
Joined: Tue Aug 31, 2010 3:34 pm
Location: Australia


Return to General

Who is online

Users browsing this forum: No registered users and 125 guests