Page 1 of 1

can't read from file

Posted: Mon Jun 22, 2015 10:35 am
by ittarter
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.

Re: can't read from file

Posted: Mon Jun 22, 2015 10:39 am
by zaxos
try

Code: Select all

put URL "file:levels.txt"
and see if you get anything in the message box

Re: can't read from file

Posted: Mon Jun 22, 2015 10:45 am
by ittarter
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...

Re: can't read from file

Posted: Mon Jun 22, 2015 10:50 am
by zaxos

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"

Re: can't read from file

Posted: Mon Jun 22, 2015 11:09 am
by ittarter
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...

Re: can't read from file

Posted: Mon Jun 22, 2015 11:22 am
by zaxos

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

Re: can't read from file

Posted: Mon Jun 22, 2015 11:24 am
by Thierry
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

Re: can't read from file

Posted: Mon Jun 22, 2015 12:33 pm
by Klaus
And again a look into the dictionary might have saved a lot of time... 8)

Re: can't read from file

Posted: Mon Jun 22, 2015 3:49 pm
by FourthWorld
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

Re: can't read from file

Posted: Tue Jun 23, 2015 12:45 pm
by ittarter
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.

Re: can't read from file

Posted: Tue Jun 23, 2015 1:18 pm
by Klaus
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

Re: can't read from file

Posted: Tue Jun 23, 2015 1:26 pm
by ittarter
Thanks for all your help and patience, everyone.

Re: can't read from file

Posted: Thu Jun 25, 2015 12:44 am
by ittarter
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.