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
-
dntknwjck
- Posts: 24
- Joined: Sat Mar 23, 2019 6:14 pm
Post
by dntknwjck » Tue Aug 27, 2019 8:19 pm
Hello -
Just trying to get this example from the dictionary to work:
read from file (field "Datafile") at -100 for charsToRead
my code is:
Code: Select all
open file (fld "txtTwo") for read
answer the result = "can't open that file"
read from file (field "txtTwo") at -101 for 400
answer the result = "file is not open for read
Haven't had any problems opening and reading disk files.
would like to read the file into memory and then be able to read it as if it were a disk file:
read from file x at 1 until "some String"
etc
Any suggestions?
thanks
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10305
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Wed Aug 28, 2019 5:05 pm
Hi.
I read and write to files here and there, so am not an expert with this syntax.
Is the problem possibly that you are trying to use chunk-like phrasing, and this might not fly in the "read" command?
By chunk-like I mean, for example, you can get char -3 to whatever of a string. But perhaps this is not valid for read?
You can always read the whole file, and then let LC do its thing. Is there any advantage to doing this beforehand?
Craig
-
jacque
- VIP Livecode Opensource Backer

- Posts: 7389
- Joined: Sat Apr 08, 2006 8:31 pm
-
Contact:
Post
by jacque » Wed Aug 28, 2019 6:13 pm
If you read the whole file into memory (i.e. into a variable) then the read commands no longer work. Use offset or other text functions instead. But reading from files is very fast, so usually there's no need to put the whole thing into memory unless you need to change something or manipulate the content in some way.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
-
jmburnod
- VIP Livecode Opensource Backer

- Posts: 2729
- Joined: Sat Dec 22, 2007 5:35 pm
-
Contact:
Post
by jmburnod » Wed Aug 28, 2019 6:56 pm
hi dntknwjck,
open file (fld "txtTwo")
"can't open that file"
What do you have in fld "txtTwo" ?
I played with "read at 1 until string" and it works here (OSX 10.13, LC 9.0.1
Code: Select all
on mouseup
doReadFileUntilString "LiveCode is great" --you have this text. in your file
end mouseup
on doReadFileUntilString pString
answer file "0pen"
put it into tFile
open file tFile for read
read from file tFile at 1 until pString
close file tFile
put it
end doReadFileUntilString
Best regards
Jean-Marc
https://alternatic.ch
-
FourthWorld
- VIP Livecode Opensource Backer

- Posts: 10043
- Joined: Sat Apr 08, 2006 7:05 am
-
Contact:
Post
by FourthWorld » Wed Aug 28, 2019 7:27 pm
dntknwjck wrote: ↑Tue Aug 27, 2019 8:19 pm
would like to read the file into memory and then be able to read it as if it were a disk file:
read from file x at 1 until "some String"
etc
Do you want to read the file into memory, or traverse it on disk?
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10305
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Wed Aug 28, 2019 8:04 pm
Unless I misunderstand, he wants to read from a file starting 101 chars from the end, going forward 400 chars.
The math is odd.
The points about reading to an LC variable or to some sort of "disk" is confusing. Several interpretations have been posted
So, dntknwjck, please restate your question.
Craig
-
dntknwjck
- Posts: 24
- Joined: Sat Mar 23, 2019 6:14 pm
Post
by dntknwjck » Thu Aug 29, 2019 4:15 pm
Thanks to all for the thoughts about this.
I have two things in mind:
1- The dictionary shows an example of reading from a field as if the field was a file. I think tat is an interesting concept and have tried to make it work, but have not been successful with that.
2- I am trying to determine the best way to repeatedly access various information from a file. Speed and ease of access are the concerning factors. Based on the information below it seems that just accessing the file repeatedly is the best solution.
jacque wrote: ↑Wed Aug 28, 2019 6:13 pm
by jacque » Wed Aug 28, 2019 11:13 am
If you read the whole file into memory (i.e. into a variable) then the read commands no longer work. Use offset or other text functions instead. But reading from files is very fast, so usually there's no need to put the whole thing into memory unless you need to change something or manipulate the content in some way.
The example I used was just copied from the dictionary reading from the end of the file. With what I am doing I would read from the begging of the file not the end.
-
jacque
- VIP Livecode Opensource Backer

- Posts: 7389
- Joined: Sat Apr 08, 2006 8:31 pm
-
Contact:
Post
by jacque » Fri Aug 30, 2019 7:00 pm
Are you referring to this example in the "Read" entry?
read from file (field "Datafile") at -100 for charsToRead
That isn't very clear, but it will only work if the content of the field is the path to the file on disk. It doesn't mean you can use the "read" command to treat the field content as though it were a file. I can see why that example would be confusing; they should have done it this way:
read from file (field "filePath") at -100 for charsToRead
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
-
dntknwjck
- Posts: 24
- Joined: Sat Mar 23, 2019 6:14 pm
Post
by dntknwjck » Sat Aug 31, 2019 5:37 pm
jacque,
Thanks for the insight about "read from file (field "Datafile") at -100 for charsToRead" in the dictionary.
Not sure I would ever have figured that out, but it does make sense.
Your example should be put in the dictionary as it is much clearer than what it currently there.