Page 1 of 1
read from file (field "Datafile") at -100 for charsToRead
Posted: Tue Aug 27, 2019 8:19 pm
by dntknwjck
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
Re: read from file (field "Datafile") at -100 for charsToRead
Posted: Wed Aug 28, 2019 5:05 pm
by dunbarx
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
Re: read from file (field "Datafile") at -100 for charsToRead
Posted: Wed Aug 28, 2019 6:13 pm
by jacque
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.
Re: read from file (field "Datafile") at -100 for charsToRead
Posted: Wed Aug 28, 2019 6:56 pm
by jmburnod
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
Re: read from file (field "Datafile") at -100 for charsToRead
Posted: Wed Aug 28, 2019 7:27 pm
by FourthWorld
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?
Re: read from file (field "Datafile") at -100 for charsToRead
Posted: Wed Aug 28, 2019 8:04 pm
by dunbarx
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
Re: read from file (field "Datafile") at -100 for charsToRead
Posted: Thu Aug 29, 2019 4:15 pm
by dntknwjck
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.
Re: read from file (field "Datafile") at -100 for charsToRead
Posted: Fri Aug 30, 2019 7:00 pm
by jacque
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
Re: read from file (field "Datafile") at -100 for charsToRead
Posted: Sat Aug 31, 2019 5:37 pm
by dntknwjck
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.