can't read from 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
ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

can't read from file

Post by ittarter » Mon Jun 22, 2015 10:35 am

Hi guys,

I've tried and tried and tried but to no avail. Why can't I do this simple task of putting some text in my .txt into a field?
I've tried the open/read commands. Nothing. Now I've tried to steamline it with the url command. Still nothing.
Any ideas? What's my problem, or how can I debug it?

Code: Select all

   put "C:/Users/Ivan/Desktop/tll audio/" into defaultfolder
   put URL "file:levels.txt" into field "fld.leveldata" ## does nothing
This is one debugging bit of code that I used:

Code: Select all

if there is a file "levels.txt" then answer "Found it!" ## yields "Found it!"
else
end if
So I know that my app can find the file... but it still can't read it. I can't yield text from my .txt file with either an answer or a put/into command.

Help is much appreciated. Let me know if you need more information.

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: can't read from file

Post by zaxos » Mon Jun 22, 2015 10:39 am

try

Code: Select all

put URL "file:levels.txt"
and see if you get anything in the message box
Knowledge is meant to be shared.

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: can't read from file

Post by ittarter » Mon Jun 22, 2015 10:45 am

Nope, nothing.

By the way, levels.txt definitely contains about 15 lines of text.

Also, don't know if it helps, but sometimes I was getting "OK" printed to the field "lbl.leveldata" -- I think that was when I was using the code "if there is a file [filename]" but it was funny because I was never asking "OK" to print anywhere...

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: can't read from file

Post by zaxos » Mon Jun 22, 2015 10:50 am

Code: Select all

set the folder to "C:/Users/Ivan/Desktop/tll audio/"
put URL "file:levels.txt" into fld "fld.leveldata"
or

Code: Select all

put URL"file:C:/Users/Ivan/Desktop/tll audio/levels.txt" into fld "fld.levelData"
these should both work, in case they dont, do some debuging:

Code: Select all

set the folder to "C:/Users/Ivan/Desktop/tll audio/"
put the folder -- to check if it is set properly
breakpoint
put the files -- to check if it finds the files
put URL "file:levels.txt" into fld "fld.leveldata"
Last edited by zaxos on Mon Jun 22, 2015 11:26 am, edited 1 time in total.
Knowledge is meant to be shared.

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: can't read from file

Post by ittarter » Mon Jun 22, 2015 11:09 am

The second code works, thanks!

The problem is resolved when I use the full file address instead of the defaultfolder variable.

I don't understand why I can't get the defaultfolder variable to work.

Code: Select all

   put "C:/Users/Ivan/Desktop/tll audio/" into defaultfolder
put URL"file:levels.txt" into fld "fld.levelData"
yields nothing. Do I explictly have to SAY defaultfolder every time I want to reference a file in that folder? All the resources I've looked at suggest that it's automatic...

zaxos
Posts: 222
Joined: Thu May 23, 2013 11:15 pm

Re: can't read from file

Post by zaxos » Mon Jun 22, 2015 11:22 am

Code: Select all

put "C:/Users/Ivan/Desktop/tll audio/" into defaultfolder
-- this is wrong, i dont see how this could work, instead:

Code: Select all

put "C:/Users/Ivan/Desktop/tll audio/" into tDefaultFolder
set the defaultFolder to tDefaultFolder
put URL"file:levels.txt" into fld "fld.levelData"
OR

Code: Select all

put "C:/Users/Ivan/Desktop/tll audio/" into tDefaultFolder
put URL ("file:"&tDefaultFolder&slash&"levels.txt) into fld "fld.levelData"
I think your problem is that you are trying to put something into defaultFolder, instead you have to set the defaultFolder like the first example
the defaultFolder is a property used by livecode, see the dictionary for more info on how to use it
Last edited by zaxos on Mon Jun 22, 2015 11:25 am, edited 1 time in total.
Knowledge is meant to be shared.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: can't read from file

Post by Thierry » Mon Jun 22, 2015 11:24 am

ittarter wrote: The problem is resolved when I use the full file address instead of the defaultfolder variable.

I don't understand why I can't get the defaultfolder variable to work.
From the dictionary, defaultfolder is a property.
Better try this instead:

Code: Select all

set the defaultfolder to "..."
HTH,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: can't read from file

Post by Klaus » Mon Jun 22, 2015 12:33 pm

And again a look into the dictionary might have saved a lot of time... 8)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: can't read from file

Post by FourthWorld » Mon Jun 22, 2015 3:49 pm

Whenever you encounter an unexpected result in LiveCode, check the value of "the result" for an indication of the cause.

With file I/O or other things dependent on the host OS, it can also be good to include a call to the sysError function.

e.g.:

Code: Select all

put url ("file:somepath/somefile.txt") into tData
if the result is not empty then
     answer "Couldn't read file: "& the results &"("& sysError() &")"
end if
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: can't read from file

Post by ittarter » Tue Jun 23, 2015 12:45 pm

Klaus wrote:And again a look into the dictionary might have saved a lot of time... 8)
Believe me, I'm in the dictionary all the time. I probably don't often understand it.

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

Re: can't read from file

Post by Klaus » Tue Jun 23, 2015 1:18 pm

ittarter wrote:
Klaus wrote:And again a look into the dictionary might have saved a lot of time... 8)
Believe me, I'm in the dictionary all the time. I probably don't often understand it.
OK, but if the example code in the dictionary and my code are THAT different, I really would think it over... 8)

Dictionary:
set the defaultfolder to XYZ
VS:
put XYZ into defaultfolder

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: can't read from file

Post by ittarter » Tue Jun 23, 2015 1:26 pm

Thanks for all your help and patience, everyone.

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: can't read from file

Post by ittarter » Thu Jun 25, 2015 12:44 am

FourthWorld wrote:Whenever you encounter an unexpected result in LiveCode, check the value of "the result" for an indication of the cause.

With file I/O or other things dependent on the host OS, it can also be good to include a call to the sysError function.

e.g.:

Code: Select all

put url ("file:somepath/somefile.txt") into tData
if the result is not empty then
     answer "Couldn't read file: "& the results &"("& sysError() &")"
end if
Thanks for the syserror code, Richard. I'll try it out tomorrow.

Post Reply