read from file not working

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
rjtheobald
Posts: 4
Joined: Thu Jan 19, 2012 3:05 am

read from file not working

Post by rjtheobald » Tue Mar 08, 2022 2:58 pm

I am trying to read a file (theFlightPlan) and put the contents into a field (flightPlanContents ) with the following mouseUp handler but the "it" variable is coming up empty - no data seems to be coming back.
I am using LiveCode version 9-5-0, running MacOS 12-2-1 Monterey, on a M1 Max processor.
I am using a button to select a file to be read (theFlightPlan) and trying to put the contents of the file into
the field flightPlanContents. What am I doing wrong are there any known bugs???

on mouseUp pButtonNumber
global inFilePath
put inFilePath & "/" & the value of the clickLine into theFlightPlan
open file theFlightPlan
read from file theFlightPlan until EOF
put it into fld flightPlanContents
close file theFlightPlan
put it into message
end mouseUp

Sample File Data
01/27/2022,01
02/24/2022,02
03/24/2022,03
04/21/2022,04
05/19/2022,05
06/16/2022,06

andresdt
Posts: 146
Joined: Fri Aug 16, 2019 7:51 pm

Re: read from file not working

Post by andresdt » Tue Mar 08, 2022 3:07 pm

I always use PUT URL

Code: Select all

PUT URL("File:" & TheflightPlan) Into Message

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: read from file not working

Post by Klaus » Tue Mar 08, 2022 3:12 pm

Hi rjtheobald,

did you check the filepath? If in doubt, I always let me ANSWER the path.
And maybe i could interest you in the short URL syntax for reading/writing files? :-)

Try this:

Code: Select all

on mouseUp pButtonNumber
   global inFilePath
   put inFilePath & "/" & the value of the clickLine into theFlightPlan
   ## Optional:
   ANSWER theFlightPlan
   put url("file:" & theFlightPlan) into fld "flightPlanContents"
end mouseUp
Hint:
1. Use QUOTES around object names, the engine is less forgiving with every version!
2. Please use the CODE tags </> after pasting your script, so the formatting will be preserved.


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9580
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: read from file not working

Post by dunbarx » Tue Mar 08, 2022 3:13 pm

Hi.

Please put all your handler offerings between the handler tags ("</>"). You can find this in the row of icons above this field. It makes for much easier reading. Just click on the icon, and the tags will appear after the cursor.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: read from file not working

Post by FourthWorld » Tue Mar 08, 2022 5:39 pm

All I/O benefits from error checking. Building a habit of checking the result after opening a file, with a call to sysError to let the OS give you the exact error code, will save you person-years of lost diagnostic time over the course of your career.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

rjtheobald
Posts: 4
Joined: Thu Jan 19, 2012 3:05 am

Re: read from file not working

Post by rjtheobald » Wed Mar 16, 2022 7:32 pm

Thank you all for responding, I found the problem. The documentation does not cover this but the behavior I observed, and what ultimately solved my problem, was that I was not opening the file for "read" and a "blank" file was being created (I don't know why) that did not have any date in it, thus my reads were coming back blank. As soon as I specifically "opened the file for read" everything worked! Perhaps some of you might know why the blank file was being created????
Thanks,
Rich.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: read from file not working

Post by FourthWorld » Wed Mar 16, 2022 8:16 pm

rjtheobald wrote:
Wed Mar 16, 2022 7:32 pm
Perhaps some of you might know why the blank file was being created????
I have no reason to mislead you: posting.php?mode=quote&f=7&p=213744#pr213463

The OS and LiveCode want to answer your questions. Let them.

"open file..." without a read/write specifier allows both. If the file exists, no new file is created. If a new file is created, and you can still see the original, logically we deduce that they have different names. Perhaps the difference is not immediately evident, such as a trailing space character. Hard to say. I would check the file name, and its origin.

And with all I/O, developing a habit of error checking will save you countless hours every year.

Code: Select all

open file theFlightPlan
if the result is not empty then
   answer "Couldn't open file "&quote& theFlightPlan &quote&: "& the result & " ("& sysError() &")"
   exit to top
 end if
That informs you of the error on first access, lets you see the path in question ("quote& theFlightPlan &quote"), lets you know LC's explanation ("the result"), and gives you the OS error code in parens at the end ("sysError()").
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”