Page 1 of 1
Writing to text file
Posted: Thu Jun 23, 2011 7:58 pm
by rclyne
Hi,
I am needing some assistance with a problem I am having when writing to a text file. The data is writing to the file okay, but where I expect to see LF (ascii value hex 0A) as the separator what I get is a CR (ascii value hex 0D). I have tried other separator combinations, for example CRLF and what I get there is ascii value hex 0D, followed by another 0D.
I am new to Livecode so don't know if I am missing something simple here.
Here is the code I have used.
Code: Select all
global arrayData
global RecordCount
global DataFileName
on mouseUp
local myText
open file DataFileName for write
repeat with iCount = 1 to RecordCount
repeat with iLoop = 1 to 11
put arrayData[iCount,iLoop] & LF into myText
write myText to file DataFileName
end repeat
end repeat
close file DataFileName
end mouseUp
Thank you in advance.
Re: Writing to text file
Posted: Thu Jun 23, 2011 9:55 pm
by Klaus
Hi rclyne,
you need to save as BINARY or LiveCode will convert all LFs to CR!
And you can use the shorter URL syntax:
global arrayData
Code: Select all
global RecordCount
global DataFileName
on mouseUp
local myText
## 1. Collecrt ALL text in a variable and....
repeat with iCount = 1 to RecordCount
repeat with iLoop = 1 to 11
put arrayData[iCount,iLoop] & LF into myText
end repeat
end repeat
## 2. ... then write to file at once
put myText into url("binfile:" & DataFileName")
end mouseUp
The read it in again with:
...
put url("binfile:" & DataFileName") into myText
...
Best
Klaus
Re: Writing to text file
Posted: Thu Jun 23, 2011 11:30 pm
by rclyne
Hi Klaus
Thank you for the quick response and it worked exactly as I needed it too.
Now that I have the code doing what I need it raises a couple of questions, and if you could answer them I would be grateful.
1) Referring to my original code. Why does it convert special characters like LF to CR? (this is more out of curiosity)
and now when I read the data back in to myText using "put url("binfile:" & DataFileName") into myText"
2) What is the best method to use to restore the data contained in "myText" back in "arrayData", when using the LF as a separator ? (I know how I would approach this using other software languages, but one thing I have found with my limited experience with Livecode, is there can be simpler solutions to old problems)
Again thank you in advance.
Richard
Re: Writing to text file
Posted: Fri Jun 24, 2011 12:27 am
by SparkOut
Actually I think that Livecode "under the hood" uses CR as the line delimiter since it is, ultimately born of the Mac stable where CR is the usual Mac OS/OS X line delimiter.
Given a text file, read or write, Livecode actually does a conversion according to the platform on which is it running. I think if you ignore setting the LF within the handler you use to create the file and stick with CR (or "return") while you are working in Livecode, and then
*don't* save as a binary file, then Livecode will make the appropriate line endings for the current platform. (Are you running this on Linux?)
If you use
Code: Select all
on mouseUp
local myText
## 1. Collect ALL text in a variable and....
repeat with iCount = 1 to RecordCount
repeat with iLoop = 1 to 11
put arrayData[iCount,iLoop] & CR into myText
-- note use of CR constant, not LF in above line
end repeat
end repeat
## 2. ... then write to file at once
put myText into url("file:" & DataFileName")
-- note use of text file type not binary type, to allow Livecode to do appropriate line ending conversion for the relevant platform
end mouseUp
That should produce a text file on any platform with that platform's native line endings encoded. When you were writing out line by line with LF as the line endings, I think that is where some problems arose, and I believe that if you had used "CR" as the delimiter in your original handler then it would also have worked to convert the line endings to LF for Linux platform (or CRLF for Windows) as appropriate.
I use CR line endings within Livecode and write out to text files all the time on Windows, where the correct char is used within the text file.
Re: Writing to text file
Posted: Fri Jun 24, 2011 1:40 am
by rclyne
Hi SparkOut
Thanks for that clarification. Now you mention this I do vaguely remember reading about Livecode's file handling, but this being the first time that I have written software using a multi-platform development system it probably shot under my radar how important the end of line delimiters where being treated depending on the OS. I will remember in the this for the future.
The application is being developed and run using Mac OS. I used LF to indicate the end of a section of data as I was unsure how the <Enter> button used within a text box would be represented in the text file, and thought I would play around with other special characters and that's when I noticed this starnge behavior.
For this project I think I will stick with binary files, again thanks for the clarification.
Best regards,
Richard.
Re: Writing to text file
Posted: Fri Jun 24, 2011 3:07 am
by jacque
SparkOut wrote:Actually I think that Livecode "under the hood" uses CR as the line delimiter since it is, ultimately born of the Mac stable where CR is the usual Mac OS/OS X line delimiter.
Actually, the engine began as a Unix port of HyperCard about 15 years ago, and so the internal line delimiter is ASCII 10 (linefeed). Trivia.
For the OP: see "return" in the dictionary for how LiveCode deals with line endings.
Re: Writing to text file
Posted: Fri Jun 24, 2011 8:30 am
by SparkOut
Well, "trivia" but informative and useful nonetheless. Thanks Jacque.
Re: Writing to text file
Posted: Fri Jun 24, 2011 8:34 am
by Klaus
Hi Jaqueline,
jacque wrote:Actually, the engine began as a Unix port of HyperCard about 15 years ago,
We're getting old, this was 20 years ago
Best
Klaus
Re: Writing to text file
Posted: Fri Jun 24, 2011 5:23 pm
by rclyne
Thanks all for the assistance with this problem. Can now happily write and read data from file. Just need one more bit of help. Once the data has been read back I have to use the "split" command to get the data back in to an array. The example in the user guide only shows single dimension array. Can it handle multi-dimension arrays, or am I going to do that bit manual?
Best regards,
Richard.