Please help how to read data from text file

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Subas
Posts: 32
Joined: Mon May 20, 2013 5:15 am

Please help how to read data from text file

Post by Subas » Sat Jun 22, 2013 7:13 am

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

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Please help how to read data from text file

Post by Simon » Sat Jun 22, 2013 7:36 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Please help how to read data from text file

Post by Dixie » Sat Jun 22, 2013 8:01 am

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...
Attachments
putgetdata.livecode.zip
(1.59 KiB) Downloaded 272 times

Subas
Posts: 32
Joined: Mon May 20, 2013 5:15 am

Re: Please help how to read data from text file

Post by Subas » Sat Jun 22, 2013 12:24 pm

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

Subas
Posts: 32
Joined: Mon May 20, 2013 5:15 am

Re: Please help how to read data from text file

Post by Subas » Sat Jun 22, 2013 12:30 pm

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

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Please help how to read data from text file

Post by Klaus » Sat Jun 22, 2013 12:36 pm

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

Subas
Posts: 32
Joined: Mon May 20, 2013 5:15 am

Re: Please help how to read data from text file

Post by Subas » Sat Jun 22, 2013 3:39 pm

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

MouseUp
Posts: 41
Joined: Sun Feb 14, 2010 3:41 pm

Re: Please help how to read data from text file

Post by MouseUp » Sat Jun 22, 2013 6:12 pm

...but what if there is a comma within the data of a field?
Thanks,

MouseUp

Dixie
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1336
Joined: Sun Jul 12, 2009 10:53 am

Re: Please help how to read data from text file

Post by Dixie » Sat Jun 22, 2013 6:23 pm

Then do not use a 'comma' as the itemDelimeter ... use say, 'tab' instead... or anorther charecter that would not appear in your data. like '¿' ...:-)

Subas
Posts: 32
Joined: Mon May 20, 2013 5:15 am

Re: Please help how to read data from text file

Post by Subas » Sun Jun 23, 2013 3:57 am

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

Post Reply