Reading from text file

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
saratogacoach
Posts: 87
Joined: Thu Jul 17, 2008 8:42 pm

Reading from text file

Post by saratogacoach » Thu Jun 25, 2009 12:00 am

Hi,

I have been trying to read the contents of a text file into a variable and cannot get the script in a button to work. I wrote the word "hello" into the first line of this text file, saving it in the default folder where the stack is located.

Code: Select all

on mouseUp
   open file "holder.txt" for read
   read from file "holder" at -1 until EOF
   put it into xTemp 
   answer xTemp
end mouseUp
My answer box is empty. I've tried other variations like read from file "holder.txt" and even put URL into xTemp, but with no success.

I must be doing something wrong.

Any help would be appreciated.

Kind Regards,
saratogacoach
LiveCode 4.6.0, Windows 7 Home Premium 64 bit, Core i7 2.80, 8G RAM, ATI Radeon HD5770

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Thu Jun 25, 2009 12:19 am

the main problem with opening files is the path, so i suggest to test wetter your file exists:

Code: Select all

if there is a file "holder.txt"
note that rev generally doesn't even know what a file extension is, so leaving it out will certainly not work.

the second problem with working with files is to use the open/read/close file commands. instead, use the url ones:

Code: Select all

on mouseUp
  answer file "a text file"
  if it = "" or the result = "cancel" then
    beep
    exit mouseUp
  end if
  put it into thePath
  if there is no file thePath then
    beep
    exit mouseUp
  end if
  put url ("file:" & thePath) into theFile
  answer theFile
end if 
I'd also suggest using "put theFile" instead of "answer theFile" but that's certainly a matter of taste...
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

saratogacoach
Posts: 87
Joined: Thu Jul 17, 2008 8:42 pm

Post by saratogacoach » Thu Jun 25, 2009 1:24 am

Hi Björnke,

Thanks for your suggestions.

What finally worked was using the URL and putting the variable into the file.

I thought I had tried that, only turning to a more convoluted approach involving opening and reading the file, when that didn't seem to work.

(I probably was not scripting the URL option correctly.) :oops:

Thanks for your help.

Kind Regards,
saratogacoach
LiveCode 4.6.0, Windows 7 Home Premium 64 bit, Core i7 2.80, 8G RAM, ATI Radeon HD5770

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

Post by Klaus » Thu Jun 25, 2009 1:08 pm

Hi Coach,

your first script was OK, but you started with the offset "... at -1" which is the end of the file, so you got nothing :-)
AND you did not add the suffix in the line: "read from file...", know what I mean?
...
open file "holder.txt"...
read from file "holder"...
..

So you were referring to two different files.


Best

Klaus

saratogacoach
Posts: 87
Joined: Thu Jul 17, 2008 8:42 pm

Post by saratogacoach » Thu Jun 25, 2009 1:27 pm

Thanks, Klaus.

Yes, I understand this better now.

(Learning this slowly but surely. :) )

Kind Regards,
saratogacoach
LiveCode 4.6.0, Windows 7 Home Premium 64 bit, Core i7 2.80, 8G RAM, ATI Radeon HD5770

Post Reply