Page 1 of 1

Saving Text Field content to file

Posted: Wed Feb 22, 2012 3:10 pm
by colinpartridge
I have a stack which puts serial data into a text field and then formats the data for import into a Filemaker database, the stack works well except if I save the raw data from the text field the resulting text file has a extra linefeed added between each line. The linefeed does not show up in the field but only if the content of the field is copied or saved out. Is this Normal? Is there any text field formatting that I may have missed which would cause this? This is the code I'm using to save the data from the field..

Code: Select all

open file rawdata
write the text of fld "RawData" to file rawdata
close file rawdata
I'm sure there is a simple solution to this but not one that is obvious to me ( an almost complete newbie to all this) cheers Colin

Image

Re: Saving Text Field content to file

Posted: Wed Feb 22, 2012 3:32 pm
by FourthWorld
Internally LiveCode always uses ASCII 10 for line endings (it was born on Unix), but using the form "open file" will cause LiveCode to write the file with OS-specific line endings, CRLF for Win, LF for Linux, and CR for Mac.

Since your screenshots show you're using a Mac I'm not sure why there's an extra LF there, which I could normally expect only on Windows.

But to be extra sure, you could use the "binary" option, and set the line-endings specific to the expected form required by the target app:

Code: Select all

open file rawdata for binary write
put the text of fld "RawData" into tData
replace numtochar(10) with numtochar(13) in tData
write tData to file rawdata
close file rawdata

Re: Saving Text Field content to file

Posted: Wed Feb 22, 2012 3:46 pm
by colinpartridge
Thanks Richard, changing the first line to

Code: Select all

open file rawdata for binary write
did the trick. Your reply regarding using the "open file" form implies there is another way ( isn't there always ) to achieve the same...I'm curious..

Colin

Re: Saving Text Field content to file

Posted: Wed Feb 22, 2012 4:02 pm
by sturgis
There is also 'put URL "binfie:/path/to/file/rawdata"' (or file:/path/to/file/rawdata, I believe 'file:' uses os specific line endings, binfile: does the write unchanged) For what you're doing ( open, write, close rather than open write write write write write, close) all at once, the URL method would work great in 1 line of code.
colinpartridge wrote:Thanks Richard, changing the first line to

Code: Select all

open file rawdata for binary write
did the trick. Your reply regarding using the "open file" form implies there is another way ( isn't there always ) to achieve the same...I'm curious..

Colin

Re: Saving Text Field content to file

Posted: Wed Feb 22, 2012 4:32 pm
by colinpartridge
sturgis wrote:There is also 'put URL "binfie:/path/to/file/rawdata"' (or file:/path/to/file/rawdata, I believe 'file:' uses os specific line endings, binfile: does the write unchanged) For what you're doing ( open, write, close rather than open write write write write write, close) all at once, the URL method would work great in 1 line of code.
colinpartridge wrote:Thanks Richard, changing the first line to

Code: Select all

open file rawdata for binary write
did the trick. Your reply regarding using the "open file" form implies there is another way ( isn't there always ) to achieve the same...I'm curious..

Colin
Yep that works too! just one line,

Code: Select all

 put the text of fld "RawData" into  URL ("binfile:" & rawdata) 
That it for learning something new today, time for a cup of tea!

Cheers

Colin