read from file

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
gilgml
Posts: 26
Joined: Wed Jun 16, 2021 3:35 am

read from file

Post by gilgml » Fri Jul 09, 2021 8:09 pm

Hello to everybody there !

I've read carefully a lot of ressources including some on this forum.

I cannot "read from file tFile for 1 line"

if i put "somePath" into tFile ( put "car3 dot txt" into tFile )
when i ( put it into temp ), temp is empty
The file exists, and sits in the same

The read is perfect if the pathname is prepared by :
answer files "Select the file you wish to process:"
and
put it into tFile

I can tell the code stuffed pathname is okay, because when i enter a wrong file name, the behavior is different ( but confusing since it returns "OK" but not the strings of the file.

Any help welcome !

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

Re: read from file

Post by Klaus » Fri Jul 09, 2021 8:57 pm

Hi gilgml (how do you pronounce that? :-) ),

welcome to the forum!

Please post your complete script, we need to take a look!


Best

Klaus

gilgml
Posts: 26
Joined: Wed Jun 16, 2021 3:35 am

Re: read from file

Post by gilgml » Sat Jul 10, 2021 12:13 am

Hi Klaus,

The gilgml nickname was meant to sound peculiar with at gmail dot com !

Code which works :
"
on mouseUp
answer files "Select the file you wish to process:"
put it into tFile
open file tFile
repeat until eof
read from file tFile for 1 line
put it into temp
if temp is empty then
exit repeat
end if
answer temp
end repeat
close file tFile
end mouseUp
"
Code not working
"
on mouseUp
put "car3_dot_txt" into tFile
open file tFile
repeat until eof
read from file tFile for 1 line
put it into temp
if temp is empty then
exit repeat
end if
answer temp
end repeat
close file tFile
end mouseUp
"

As you can see, the only difference is the way the pathname is built.
_dot_ replaces a dot in the string since phpBB refuses the true string.
The file exists and sits next to the stack file.

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

Re: read from file

Post by FourthWorld » Sat Jul 10, 2021 1:06 am

gilgml wrote:
Sat Jul 10, 2021 12:13 am
As you can see, the only difference is the way the pathname is built.
_dot_ replaces a dot in the string since phpBB refuses the true string.
The file exists and sits next to the stack file.
I/O without error checking is driving blindfolded.

After open file, check "the result". Bonus points for adding a call to sysError when reporting if the result is not the empty.

Also, the working script uses an absolute path, while the failing one is relative, dependent on an assumption of the current folder...
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: read from file

Post by dunbarx » Sat Jul 10, 2021 3:57 am

gligmi

Please put your handlers within the tags ("</>") that can be found in the icon strip at the top of the field. It makes it much easier for us to read.

Like this:

Code: Select all

on mouseUp
end mouseUp
Craig

gilgml
Posts: 26
Joined: Wed Jun 16, 2021 3:35 am

Re: read from file

Post by gilgml » Sat Jul 10, 2021 10:30 am

I/O without error checking is driving blindfolded.
Oh yes, of course, but i did not see strong recommendations on how to do it properly in the various examples of code i went through. I was waiting to do so, and i am pretty sure it will reveal the problem !
So now i code :

Code: Select all

   
on mouseUp

   local tFile, temp
      
   put "car3 dot txt" into tFile
   open file tFile
   if the sysError is not zero then
      answer "Problem opening : " & the sysError
   else
      repeat until eof
         read from file tFile for 1 line 
         put it into temp
         if temp is empty then
            exit repeat
         end if
         answer "Line fetched " & temp
      end repeat
   end if 
   close file tFile
end mouseUp
Nothing appears, so i presume sysError is 0.
But the lines are still not fetched :shock:

Thank you for your patience.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: read from file

Post by jmburnod » Sat Jul 10, 2021 1:21 pm

Hi,
I seems that is a path issue.
You may try this :
Rename file name of your target file to "car3.txt"
Put "car3.txt" file into documens folder

Code: Select all

on mouseup
   local tFile, temp
   put specialfolderpath(documents) into tPathFol
   set the defaultfolder to tPathFol
   put tPathFol & "/" & "car3.txt" into tFile
   -- answer (there is a file tFile)
   open file tFile
   repeat until eof
      read from file tFile for 1 line 
      put it into temp
      if temp is empty then
         exit repeat
      end if
      answer temp
   end repeat
   close file tFile
end mouseup
Best regards
Jean-Marc
https://alternatic.ch

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

Re: read from file

Post by Klaus » Sat Jul 10, 2021 1:22 pm

Hi gilgml,

as Richard already ponted out, if you do not provide an ABSOLUTE filepath, in this case just the filename,
then LC will only check the content of -> the defaultfolder, whereever that may be!
And fails if that file is not present.

So you need to check first if that file is present!
Something like:

Code: Select all

...
put "car3.txt" into tFile

## First check!
if there is NOT a file tFile then

     ## Complain and show the current folder (optional, but may help you find the problem)
      answer "Problem!" & CR "File not present in current folder:" & CR & the defaultfolder
      exit mouseup
 end if

## File present, now do your thing:
open file tFile
 ...
Hint:
With your "file read" code you will end with only the LAST line of the file, because you overwrite
the content of TEMP in every repeat loop!

Code: Select all

...
repeat until eof
         read from file tFile for 1 line 
         ## This line:
         put it into temp
         if temp is empty then
            exit repeat
         end if
         answer "Line fetched " & temp
end repeat
...
Best

Klaus

gilgml
Posts: 26
Joined: Wed Jun 16, 2021 3:35 am

Re: read from file

Post by gilgml » Sun Jul 11, 2021 11:01 am

@klaus

The test

Code: Select all

 if there is NOT a file tFile then 
is run, and the file is present ( the answer text is issued only if i code a wrong filename )
Since i read somewhere that there is no need to specify complex pathnames is the file is beside the stack, i took the lazy way.
It is not the issue.
But the code you provided taught me a lot ! Thank you !
With your "file read" code you will end with only the LAST line of the file, because you overwrite
the content of TEMP in every repeat loop!
Sure thing, this portion of code is only intended to reveal and isolate one line after the other in the 'answer'. I'll deal with them later ! Thank you !

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: read from file

Post by SparkOut » Sun Jul 11, 2021 11:51 am

gilgml wrote:
Sun Jul 11, 2021 11:01 am
Since i read somewhere that there is no need to specify complex pathnames is the file is beside the stack, i took the lazy way.
The "beside the stack" is an oversimplification, which really means "in the defaultFolder". The location of the defaultFolder starts out as being relative to the location of the engine. In a compiled standalone, this will be "beside the stack" but in the IDE it will be where the LiveCode installation was made. (The location may be changed.)

As Klaus says, check the defaultFolder to find where your code is looking for the data file.

I am going to make a guess that you are running this in the IDE and in your previous tests before error-trapping, your code to open the file has created an empty file in the components folder. This is now not showing an error when you check, but it is not the valid data file you really want.

Also, I don't know how large the data file is, but if you are answering every line, I suspect it is pretty small, so you might find it a lot simpler to just

Code: Select all

put url ("file:" & tFile) into tData
in one go, and just work on the data lines in your repeat loop. In any event, make sure the defaultFolder is correct first.

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

Re: read from file

Post by Klaus » Sun Jul 11, 2021 2:37 pm

Hi gilgml,
...the file is beside the stack
AHA, important piece of information! :-)
In that case you can use -> specialfolderpath("resources")
While in the IDE it will give you the folder where the stack resides in, and here your TXT file also:

Code: Select all

...
put specialfolderpath("resources") & "/car3.txt" into tFile
...
When you create a runtime/standalone from your stack, that will return the correct folder,
where you will find all the files and folders that you add to your standalone via the "Copy files" tab
in the "Standalone Application Settings", whereever that may be on the target platform, very helpful!


Best

Klaus

gilgml
Posts: 26
Joined: Wed Jun 16, 2021 3:35 am

Re: read from file

Post by gilgml » Mon Jul 12, 2021 1:01 am

@sparkout

Okay, it is a simplification... I understand your point, and was lazy.
I am working in the IDE not in a standalone, since i am a beginner.
I am going to make a guess that you are running this in the IDE and in your previous tests before error-trapping, your code to open the file has created an empty file in the components folder. This is now not showing an error when you check, but it is not the valid data file you really want.
But i cannot see the file you predict in the folder where the IDE executable is ( /Applications on a Mac ). Perhaps because previous attempts of the code were made using

Code: Select all

open file tFile for read
Anyway, i followed your advices, and coded prefixing with specialfolderpath("resources") like Klaus told.
:lol: :P
I can read the files now !

Many thanks to all of you :wink: :oops:

I understand what you mean when you advice to

Code: Select all

put url ("file:" & tFile) into tData
And i saw this examples when searching a few days ago.

But i deal with small files and won't take the time to learn how to work the data fetched in one chunk, because i am so happy to follow my coding with the help i received :mrgreen:

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

Re: read from file

Post by FourthWorld » Fri Jul 16, 2021 5:49 am

gilgml wrote:
Mon Jul 12, 2021 1:01 am
I can read the files now !

Many thanks to all of you :wink: :oops:
Glad you got that sorted.

To clarify my note about checking "the result" and including a call to sysError, here's the code I wish someone had shared with me when I was starting out:

Code: Select all

put url ("file:"& tFile) into tVar -- or any other file I/O, like "open file..."
if the result is not empty then
   answer "Couldn't read file: "& the result &" (" &sysError() &")"
   exit to top 
end if 
That first checks "the result", LC's catch-all for many types of errors. If non-empty we know it contains an error description, so we report that to the user with the OS-provided error code from sysError in parentheses.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: read from file

Post by richmond62 » Fri Jul 16, 2021 7:33 am

Slightly OT, but today in my summer LiveCode classes for small "prawns" we are going to be
playing around with a few comma delimited text files exported from LibreOffice and I intend to
use this script to get things rolling, in a button:

Code: Select all

on mouseUp
   answer file "Choose a TEXT file to import"
   if the result = "cancel" 
   then exit mouseUp
   else
      set the text of fld "rawText" to URL ("file:" & it)
   end if
end mouseUp
No need to get "all sweaty" about file paths with that one. 8)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”