Page 1 of 1

Please help how to read data from text file

Posted: Sat Jun 22, 2013 7:13 am
by Subas
Dear Livecode gurus.

I have managed to do the following where I now can get 4 fields from my main stack to be saved
into a text file on the desktop based on the below codes thanks to everyone that help wit their ideas

Code: Select all


on mouseUp
   
  put specialFolderPath("desktop") & "/vr.txt" into myFile

   open file myFile for write

write fld "name"  to file myFile
write fld "foname" & return to file myFile
write fld "staffno" to file myFile
write fld "dept" & return to file myFile
close file myFile

end mouseUp

 
from the above I get the following on my vr.txt file as per below

samkevin0202384qa

if possible how do I get the fields in the text file to show the below

sam,kevin,0202384,qa

I tried using the delimiter option but some how it did not work as I am sure I am using it the wrong way.

Also, I have managed to read the vr.txt by clicking a button as per below code file but I am having problem as I only know how to read these back to only one field called Varray below.

what I need is to read the vr.txt file back to the individual fields in the main stack that produce the data initially.

Code: Select all


on mouseUp
   
   open file specialFolderPath("desktop") & "/vr.txt"
   read from file specialFolderPath("desktop") & "/vr.txt" until EOF
   put it into fld "vArray"
   close file specialFolderPath("desktop") & "/vr.txt" 

end mouseUp

the number of fields for my demo will be below 100 fields. for testing purposes I am using only 4 as above.

Really appreciate if the livecode gurus can help me move forward in my coding.

Thank you very much
subas

Re: Please help how to read data from text file

Posted: Sat Jun 22, 2013 7:36 am
by Simon
Hi Subas,
I'm not a guru but can I give it a shot?
When I first started with LC I fell into the "write to file" thing. There is easier ways of doing it in your case.

Code: Select all

on mouseUp
   set the default folder to specialFolderPath("desktop")
   put fld "name"  & comma into myVar
   put fld "foname" & comma after myVar
   put fld "staffno" & comma after myVar
   put fld "dept" & comma after myVar
   put myVar into url "file:vr.txt"
end mouseUp
That will give you the values in the fields like this:
"name,foname,staffno,dept" in a file on your desktop named vr.txt

Then reading it:
set the default folder to specialFolderPath("desktop")
put url "file:vr.txt" into myArray --not really an array

Simon

Re: Please help how to read data from text file

Posted: Sat Jun 22, 2013 8:01 am
by Dixie
Attached a stack... It will let you enter your data and then read from the text file...
All the handlers are in the card script...

Re: Please help how to read data from text file

Posted: Sat Jun 22, 2013 12:24 pm
by Subas
daer simon.

it work's. now my data get's it in this way below:

subas,kevin,0202384,

perfect. now I need to know who to read them back to the original fields that created them.

thanks for the help.

best regards
subas

Re: Please help how to read data from text file

Posted: Sat Jun 22, 2013 12:30 pm
by Subas
dear Dixie.

thank you for the stack. I ran the stack and it work's. just one question, when you read the file back it goes to only shows in the receiving field. what I need if you can help is to send those data from the file back to their original 3 fields.

example if the Field Name created "subas", staff no created 0202384 and field age created "50" then when we write it to text it will show up as

subas,0202384,50

which is currently working but I want to read that text file(vr.txt) to place the data subas back to field name place 0202384 back to field staff no and 50 back to field age in the main stack.

could you please help. thanks

best regards
subas

Re: Please help how to read data from text file

Posted: Sat Jun 22, 2013 12:36 pm
by Klaus
Hi subas,

since the content of the prefs file is a COMMA delimited list, you can access any item of it and put it into the correct field!

Example:
...
## tVar contains the file content: subas,0202384,50
put item 1 of tVar into fld "name"
put item 2 of tvar into fld "staffno"
put item 3 of tVar into fld "dept"
## If that is the correct order :-)
...


Best

Klaus

Re: Please help how to read data from text file

Posted: Sat Jun 22, 2013 3:39 pm
by Subas
dear Klaus

thank you for the information.
I tested it and it works perfectly.

Also, many thanks to the rest that helped.

I can now move on to the next phase.

Best Regards
Subas

Re: Please help how to read data from text file

Posted: Sat Jun 22, 2013 6:12 pm
by MouseUp
...but what if there is a comma within the data of a field?

Re: Please help how to read data from text file

Posted: Sat Jun 22, 2013 6:23 pm
by Dixie
Then do not use a 'comma' as the itemDelimeter ... use say, 'tab' instead... or anorther charecter that would not appear in your data. like '¿' ...:-)

Re: Please help how to read data from text file

Posted: Sun Jun 23, 2013 3:57 am
by Subas
Dear mouseup.

I analysis the data from my input fields and I know there will never me a comma in the data as such that why I decided to use comma delimited as an output. But, if there is then I will use the suggestion given by Dixie.

Best Regards
Subas