Writing to a file in DOS format [SOLVED]

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

Writing to a file in DOS format [SOLVED]

Post by andrewferguson » Tue Aug 25, 2015 7:42 am

Hello,

I am trying to use LiveCode to generate some text files that will then be run through a third party tool, but I have run into a problem. On a Mac (what I am currently trying to use) LiveCode seems to write text in the MAC formatting. However, the third party tool only works with files that have been written in the DOS formatting.

The difference between the two files seem to be very little: MAC formatting uses cartridge return as return, whereas DOS uses cartridge return and then linefeed as return. I tried:

Code: Select all

open file theFile for read
read from file theFile until eof
put it into tData
close file theFile
replace cr with cr & linefeed in tData
open file theFile for write
write tData to file theFile
close file theFile
and while this should work, it does not.

So does anyone know how I can force LiveCode to write in DOS formatting, or is there a way to convert the file from MAC to DOS formatting? (I am happy to use the built in commands that come with Mac OS X, but I cannot use any other tools because I don't have the administrator privileges required to run them. I can, however, use MS Word / Textedit and would be happy to use LiveCode's do xxx as applescript in order to convert the files.)

Thanks,
Andrew
Last edited by andrewferguson on Tue Aug 25, 2015 1:44 pm, edited 1 time in total.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Writing to a file in DOS format

Post by jmburnod » Tue Aug 25, 2015 7:54 am

Hi Andrew,

What about MacToIso ?
Best regards
Jean-Marc
https://alternatic.ch

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Writing to a file in DOS format

Post by FourthWorld » Tue Aug 25, 2015 8:00 am

By default LC will read files in a way that translates platform-specific line endings into its internal form that uses the Unix convention (0x10), and when writing it'll translate its internal form into whatever's appropriate for the platform it's running on (0x13 for Mac, 0x10+0x13 for Windows, and leave it as 0x10 for Linux).

While that may be convenient most of the time, sometimes, as in your case, you want a specific output format that may be different than the convention used on the platform it's running on. No problem - LiveCode always has options.

For reading or writing open the file as binary, e.g.:

Code: Select all

open file tFile for binary write
If using the "put url" method, specify "binfile" instead of "file":

Code: Select all

put tData into url ("binfile:"& tFile)
I should add that since the internal format always uses the Unix convention, you'll want to translate line endings before you write, where you'll probably find it most useful to use the constants provided for that:

Code: Select all

replace CR with CRLF in tData
put tData into url ["binfile:"& tFile)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

andrewferguson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 184
Joined: Wed Apr 10, 2013 5:09 pm

Re: Writing to a file in DOS format

Post by andrewferguson » Tue Aug 25, 2015 1:44 pm

FourthWorld wrote: If using the "put url" method, specify "binfile" instead of "file":

Code: Select all

put tData into url ("binfile:"& tFile)
Hi Richard,
Thanks! That method worked perfectly!
Andrew

Post Reply

Return to “Talking LiveCode”