Page 1 of 1

Can't read from text file (no data in "it")

Posted: Wed Oct 06, 2010 8:58 am
by MichaelBluejay
Here's my code:

Code: Select all

  put "randomnumbers.txt" into theFile
   open file theFile for read
   read from file theFile until EOF
   answer the result
   close file theFile
   answer the result
   answer char 1 to 10 of it
The result of the read is "EOF".
The result of the close command is empty.
Char 1 to 10 of the "it" variable shows "OK". (The first ten characters of the file are actually 1111100110.)

What am I doing wrong?

Re: Can't read from text file (no data in "it")

Posted: Wed Oct 06, 2010 10:06 am
by Klaus
Hi Michael,

problem is that the variable IT changes when you least exspect it! :D
After every ANSWER dialog IT will contain the button clicked in the dialog and thus the last content of IT is overwritten!
So you should put IT into some variable immediately!

Use the URL syntax, which is also much shorter:

Code: Select all

...
## Look ma, no IT!  :-D

## Put the content of the file into a variable with one line!
put url("file:" &  "randomnumbers.txt") into theFile

## This will give a hint on what might have gone wrong
if the result <> empty then
    answer the result
end if
answer char 1 to 10 of theFile
...
Best

Klaus

Re: Can't read from text file (no data in "it")

Posted: Wed Oct 06, 2010 8:06 pm
by MichaelBluejay
Thanks very much Klaus. I forgot about "it" changing, but really, the code wasn't working even *before* I added the "answer" commands to try to debug. The failure to get the contents of the file was what *caused* me to add the "answer" commands.

I tried your code and it fails too. I get one dialog box, which is completely empty. That suggests that there is no error ("result" is empty), but I still see no data. What do I try now?

Re: Can't read from text file (no data in "it")

Posted: Wed Oct 06, 2010 9:08 pm
by Klaus
Hi Michael,

hmm, no idea so far.
But the file can be opened with a text editor and is not empty?

You could send me the file and I can take a look if you like:
klaus AT major-k.de


Best

Klaus

Re: Can't read from text file (no data in "it")

Posted: Thu Oct 07, 2010 3:57 pm
by Regulae
Hi there,

Perhaps you might try the following:

Code: Select all

on mouseUp
   put "randomnumbers.txt" into theFile
   open file theFile for read
   read from file theFile until EOF
   put it into myFileText
   close file theFile
   answer char 1 to 10 of myFileText
end mouseUp
... which I put in a button, having first created a text file called “randomnumbers” and typed in some text. The extension “.txt” was added automatically by the Notepad program I used to create the file. I saved the file in the same folder as the stack in which I tested the button, so this was the defaultfolder which meant Rev had no trouble finding the file. I took the clue from what Klaus said about storing “it” in a variable immediately after the “read”.

Regards,
Michael

Re: Can't read from text file (no data in "it")

Posted: Fri Oct 08, 2010 9:29 am
by MichaelBluejay
Thanks, Regulae, but that didn't work either.

I finally got it to work by specifying a full, absolute path:

put url("file:/Gambling/sims/randomnumbers.txt") into fld 1

But my text file is in the same folder as the stack, so I don't know why LiveCode can't find it unless I specify a full path. This solution is less than ideal, since I don't know what the path will be on anyone else's computer. So how do I tell LiveCode to just look in the same folder for the file? I tried "file:./randomnumbers.txt" but that didn't work. I'm on Max OS X 10.6, by the way, if that matters.

Re: Can't read from text file (no data in "it")

Posted: Fri Oct 08, 2010 9:51 am
by Regulae
Hi Michael,

This is a thought more in hope than expectation, but possibly getting the long name of the stack, extracting the path to the folder in which it resides, then setting the defaultFolder to the path, might get you somewhere. That might tell Rev where to look. Off the top of my head, if you put the long name of the stack into a variable, set the itemDelimiter to “/”, then delete the last item, it should leave you with the path to the folder. I’m working under Windows XP, and though fond of Macs, I don’t have an OS X Mac to test on at the moment, but it’s something for you to try.

Regards,
Michael

Re: Can't read from text file (no data in "it")

Posted: Fri Oct 08, 2010 10:24 am
by Klaus
Hi Michael,

YOU are the developer, so YOU will have to make it work! :D

Using/setting the defaultfolder is not reliable!
Write a little function that will return the absolute path to the current stack as Reguiale said and that's it!

Code: Select all

function Folder_where_this_stack_resides_in
  put the effective filename of this stack into tPath
  ## substacks do not have a filenmae, so this (th eeffective filename) will also work for substacks
  ## since it returns the filename of their mainstack!
  
  set the itemdel to "/"
  delete item -1 of tPath
  return tPath & "/"
end Folder_where_this_stack_resides_in
Now you can do:
...
put Folder_where_this_stack_resides_in() & "randomnumbers.txt" into tFile
...
Will work on any machine on any platform!


Best

Klaus


P.S.
You received my mail, didn't you?

Re: Can't read from text file (no data in "it")

Posted: Fri Oct 08, 2010 7:47 pm
by MichaelBluejay
Thanks everyone for your help! I forgot to mention, I was thinking along the same lines (find out where the stack is and extract the path from that), but I couldn't find the property I needed in the Dictionary. I was expecting it to be something like "path", "pathname", "location", etc., and just accidentally overlooked "filename". Anyway, yes, this works fine. Thanks!

Re: Can't read from text file (no data in "it")

Posted: Tue Oct 19, 2010 1:02 pm
by peter.s
Hi,

I don't mean to divert this post, but I was reading Klaus' script above and have a question about "function".

I read up about Functions in the dictionary, but I am still a bit fuzzy about where to use them (or in this case, where to use Klaus' above script). For example would this function be in the stack script, and therefore any card script will simply use the function name to utelise the effective filename of where the stack resides?

The reason I am asking is because the project I am working on will need to access images, PDFs and video files from a folder. The standalone and the folder will reside on a CD/DVD. For my stack to find the files, I assume I will need to provide the path in development. Therefore, could (should) I be using a script like what Klaus has suggested above?

Cheers,

Peter

Re: Can't read from text file (no data in "it")

Posted: Tue Oct 19, 2010 1:09 pm
by Klaus
Hi Peter,
peter.s wrote:...
I read up about Functions in the dictionary, but I am still a bit fuzzy about where to use them (or in this case, where to use Klaus' above script). For example would this function be in the stack script, and therefore any card script will simply use the function name to utelise the effective filename of where the stack resides?
Since this function does not need to access any object on any card, the stackscript (of your mainstack) will be ideal place to put it!
All objects and cards of the mainstack and any substack can access it then.
peter.s wrote:...
The reason I am asking is because the project I am working on will need to access images, PDFs and video files from a folder. The standalone and the folder will reside on a CD/DVD. For my stack to find the files, I assume I will need to provide the path in development. Therefore, could (should) I be using a script like what Klaus has suggested above?
Yes, absolutely. This way you can ensure that all media files will be found!

Best

Klaus

Re: Can't read from text file (no data in "it")

Posted: Tue Oct 19, 2010 10:20 pm
by peter.s
Thanks for confirming this Klaus!

Peter