read from file not working
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 4
- Joined: Thu Jan 19, 2012 3:05 am
read from file not working
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
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
Re: read from file not working
I always use PUT URL
Code: Select all
PUT URL("File:" & TheflightPlan) Into Message
Re: read from file not working
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:
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
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9580
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: read from file not working
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9802
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: read from file not working
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Posts: 4
- Joined: Thu Jan 19, 2012 3:05 am
Re: read from file not working
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.
Thanks,
Rich.
-
- VIP Livecode Opensource Backer
- Posts: 9802
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: read from file not working
I have no reason to mislead you: posting.php?mode=quote&f=7&p=213744#pr213463rjtheobald wrote: ↑Wed Mar 16, 2022 7:32 pmPerhaps some of you might know why the blank file was being created????
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 ""e& theFlightPlan "e&: "& the result & " ("& sysError() &")"
exit to top
end if
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn