Saving Text Field content to file

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

Post Reply
colinpartridge
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9
Joined: Sat Jan 28, 2012 1:25 pm

Saving Text Field content to file

Post by colinpartridge » Wed Feb 22, 2012 3:10 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10049
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Saving Text Field content to file

Post by FourthWorld » Wed Feb 22, 2012 3:32 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

colinpartridge
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9
Joined: Sat Jan 28, 2012 1:25 pm

Re: Saving Text Field content to file

Post by colinpartridge » Wed Feb 22, 2012 3:46 pm

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Saving Text Field content to file

Post by sturgis » Wed Feb 22, 2012 4:02 pm

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

colinpartridge
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9
Joined: Sat Jan 28, 2012 1:25 pm

Re: Saving Text Field content to file

Post by colinpartridge » Wed Feb 22, 2012 4:32 pm

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

Post Reply