read from file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
read from file
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 !
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 !
Re: read from file
Hi gilgml (how do you pronounce that? ),
welcome to the forum!
Please post your complete script, we need to take a look!
Best
Klaus
welcome to the forum!
Please post your complete script, we need to take a look!
Best
Klaus
Re: read from file
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9823
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: read from 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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: read from file
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:
Craig
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
Re: read from file
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 !I/O without error checking is driving blindfolded.
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
But the lines are still not fetched
Thank you for your patience.
-
- VIP Livecode Opensource Backer
- Posts: 2718
- Joined: Sat Dec 22, 2007 5:35 pm
- Location: Genève
- Contact:
Re: read from file
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
Best regards
Jean-Marc
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
Jean-Marc
https://alternatic.ch
Re: read from file
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:
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!
Best
Klaus
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
...
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
...
Klaus
Re: read from file
@klaus
The test
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 !
The test
Code: Select all
if there is NOT a file tFile then
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 !
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 !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!
Re: read from file
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
Re: read from file
Hi gilgml,
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:
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
AHA, important piece of information!...the file is beside the stack
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
...
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
Re: read from file
@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.
Anyway, i followed your advices, and coded prefixing with specialfolderpath("resources") like Klaus told.
I can read the files now !
Many thanks to all of you
I understand what you mean when you advice to
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
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.
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 usingI 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.
Code: Select all
open file tFile for read
I can read the files now !
Many thanks to all of you
I understand what you mean when you advice to
Code: Select all
put url ("file:" & tFile) into tData
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
-
- VIP Livecode Opensource Backer
- Posts: 9823
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: read from file
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
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
-
- Livecode Opensource Backer
- Posts: 9359
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: read from file
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:
No need to get "all sweaty" about file paths with that one.
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