Page 1 of 1
newbie question; write to file, read from file?
Posted: Tue Jul 10, 2007 1:59 am
by homer
I'm sure I'm missing something in the 'grammer' of putting information into a file.
I tried to first "open" a file.
I made a button to test and did "write "hello" to file "test111.txt"
then, "write "Hello again" to file "test111.txt"
what happens is that when I open the txt file i get:
"helloHello again"
How do I specify carriage return?
How do I specify which line of the file to write to?
and
How do I 'retrieve' the info? "read line 4 of file "test111.txt"???
Thanks in advance. More questions to follow.
homer
Posted: Tue Jul 10, 2007 6:28 am
by Janschenkel
Hi Homer,
The line separator differs between platforms: Unix prefers linefeed (ASCII 10), Mac Classic prefers carriage return (ASCII 13, and Windows prefers carriageReturn+lineFeed (ASCII 13+10).
When you use the 'URL' keyword to create files, Revolution will take care of this for you:
Code: Select all
put theDataToExport into URL ("file:" & theFilePath)
The good news is that it also does this the other way around:
Code: Select all
put URL ("file:" & theFilePath) into theImportedData
However, you can always use the 'write to file' command:
Code: Select all
write "hello" & numToChar(13) to file theFilePath
write "Hello again" to file theFilePath
Hope this helped,
Jan Schenkel.
Posted: Tue Jul 10, 2007 6:58 am
by kpeters
Homer ~
in addition to that, Rev also defines a few constants for you:
Look up Return & CRLF in the help.
HTH,
Kai
Posted: Tue Jul 10, 2007 12:06 pm
by Mark Smith
And I believe that revolution will actually use the right newline character for whatever platform it's running on, whichever constant you use.
I always use 'cr', and Revolution seems to do the right thing...
Best,
Mark
Re: newbie question; write to file, read from file?
Posted: Tue Jul 10, 2007 3:32 pm
by xApple
homer wrote:.How do I specify which line of the file to write to?
You're better off making all the changes you want to your data and writing it all in one go to the file once it's ready. Trying to change the file as the data evolves is tricky. I have posted my standard handlers for reading and writing to a file here if you are interested:
http://forums.runrev.com/phpBB2/viewtopic.php?t=903
write to file
Posted: Tue Jul 10, 2007 6:39 pm
by homer
thanks to all for these suggestions.
I'm just learning. I wanted to do the "long way" around to learn and understand things a little better. Also, I am attempting to build an application that uses input from a user. I want to store the user input into a 'data' file, and then retrieve the data at another time.
So, I thought I could write the date to specific lines in a txt file:
Line 1 = value 1
Line 2 = value 2
and so forth.
if the user changes a value, the new value would replace the old value.
so, I need to write the new value 1 over the old value one.
'write "MyVariable2" to line 2 of file "data.txt"???
'write "MyVariable3" to line 3 of file "data.txt"???
on OpenStack
put line 1 of file "data.txt" into "MyVariable1"
put line 2 of file "data.txt" into "MyVariable2"
... etc...
What I would like to do is build a stand alone application. Since Rev is not used to run the stand alone app., how else do I keep a record of the input values?
Thanks, I'll keep at it.
Further suggestions appreciated.
Homer
Posted: Tue Jul 10, 2007 9:27 pm
by xApple
Using a file to store you data is only necessary once your applications quits (in order for it to reload the data next time you open it). As the data changes
while the application is open, there is no need to write information to the disk. Keep it in a global variable. And write all the changes upon shutdown.
To store values that are in fields that need to be conserved after closing, I have posted my scripts here:
http://forums.runrev.com/phpBB2/viewtopic.php?t=735
A small excerpt form the rev documentation concerning this problem can be found here:
http://forums.runrev.com/phpBB2/viewtopic.php?t=758
Probably the easiest way to keep your data across applications uses is to put nothing into your mainstack and build your project in as many sub stacks as you want. As long as when building you check the option "Move substacks into individual stackfiles" these can be saved by your standalone as any Revolution stack usualy is.
Posted: Sat Jul 14, 2007 2:59 am
by homer
too complex for my limited knowledge. but thanks.
I want to build a stand alone app.
field one changes.
I want to store contents of field one in a data.txt file
I when the field changes during usage of the app, the data will change.
why. why why. why does this not work?
"put line one of fieild one into line one of data.txt file.
"put line two of field one into line two of data.txt file.
why can't someone help me with something 'simple'
I don't know your code stuff. i don't want to store preferences, i want to store data. what do i care about preferences at this point of my learning?
thanks anyway.
is there any other forums for Revolution. this is not much help.
Posted: Sat Jul 14, 2007 8:13 am
by Klaus
Hi homer,
please don't give up too soon, help is on its way!
This is what you want, its not very efficient, but effiency will come to your code later after you understood all the basics
The basic syntax for writing contents to textfiles is like this:
(If you use "write ... to file ..." then you MUST "open" this file for write first and after writing to the file you MUST close this file again! So the following syntax is much easier to use AND understand)
...
put line 1 of field 1 into line 1 of url("file:" & "data.txt")
put line 2 of field 2 into line 2 of url("file:" & "data.txt")
## etc...
...
Et voila, that's it
Here some more hints:
To avoid too many write actions on disc, you could collect your data first into a variable and then write it to a file in one piece.
...
put line 1 of field 1 into line 1 of Var_that_holds_all_data
## You can of course use a shorter name for this variable

put line 2 of field 2 into line 2 of Var_that_holds_all_data
put line 3 of field 3 into line 3 of Var_that_holds_all_data
...
put Var_that_holds_all_data url("file:" & "data.txt")
...
To retrieve info from a file on disk, you can use the same syntax:
...
put line 4 of url("file:" & "data.txt") into fld 4
...
And the same as above applies here.
If you need more info from the file, you should put the complete file into a variable first and then use this variable. This also minimizes the acces to the harddisk and will be much faster if you need a lot of data:
...
put url("file:" & "data.txt") into another_variable
put line 1 of another_variable into fld 1
put line 2 of another_variable into fld 2
put line 3 of another_variable into fld 3
## etc...
Hope that helps.
Best from germany
Klaus
Posted: Sun Jul 15, 2007 5:19 pm
by xApple
homer wrote:i don't want to store preferences, i want to store data. what do i care about preferences at this point of my learning?
It's the same thing: a preference is a piece of data. But as I said you'd better switch to the sub-stack method, since as your project will get bigger you might have more fields/data/preferences (call them as you want) to store. With the file method you will need to constantly complexify your script while the sub-stack method will record any changes (however numerous they are) in your interface like RunRev does.
Sorry this is the only Rev forum to my knowledge.
Cheers, xApple.
Posted: Mon Jul 16, 2007 4:42 am
by homer
thank you for your patience and suggestions.
I know that the more sophisticated method is best... in the end... however i need to go slow and understand the "flow" of the information.
My plan is to have to have basically two card, maybe three and to save the 'stack' as a stand alone. it is my understanding that a stand alone app. will not store changes. so the plan... for my beginning steps is to save the data to a file, and have a script that opens the data file when the app is started.
the first card is where the user will be. the second card is where i plan to put the data into fields. the user never sees this data card. it's just there to store it.
I will try the save to file methods and see what happens.
thanks again for suggestions.
homer
Posted: Wed Jul 18, 2007 1:28 am
by homer
i dont get the 'url' thing.
why does this not work?
on mouseUp
get line 1 of field "Field1"
open file url("c:\data33.txt")
put line 1 of field "Field1" into line 1 of url("c:\data33.txt")
close file url("c:\data33.txt")
end mouseUp
when i examine the file which is created, there is nothing there.
what am i missing?
homer
Posted: Wed Jul 18, 2007 5:31 am
by Janschenkel
Hi Homer,
You don't need to open and close URL's. When you 'put' something into a URL or 'get' something from a URL, Revolution will open the file, write/read the portion of data, and close the file.
Also, you need to specify a URL protocol: file:, binfile:, ftp:// or http://
And Revolution likes its paths with slashes (/), not a backslashes (\) - a leftover from its Unix past.
So you could replace your code with:
Code: Select all
on mouseUp
put "c:/data33.txt" into tFilePath
put "file:" & tFilePath into tURL
put line 1 of field "Field1" into line 1 of URL tURL
end mouseUp
(Technically it' can be done on one line, but I wanted to emphasize the parts of the URL construction)
Hope this helped,
Jan Schenkel.