[racket] Question about open-output-file

From: Michael Coppola (coppola.mi at husky.neu.edu)
Date: Sun Dec 5 03:01:09 EST 2010

Hi everyone,

I am using the function "open-output-file" in conjunction with
"copy-port" to take incoming data from a TCP port and write it to a
file, but I am running into an issue.  In my code, I offer the user the
option of saving the file by means of "binary" or "ASCII," the first
meaning that data is written exactly as it is received, and the latter
meaning that the incoming data is (most likely) a text file that should
be converted from one OS format to another (i.e., a file from *nix will
have LF for line termination, and it must be converted to CRLF if being
sent to Windows).

open-output-file offers the option #:mode that lets me specify either
'text (for ASCII mode) or 'binary (for, you know).  Naturally, using
'binary produces the exact result I want, but using 'text is not
converting the line terminators the way I want.

For instance, if the incoming text is:

this is line one[CRLF]
this is line two[CRLF]
this is line three

then open-output-port will write the data as:

this is line one[CRCRLF]
this is line two[CRCRLF]
this is line three

I want it to recognize that it does not have to be converted since it is
already in its native format.  The same would go for transferring to a
*nix machine, where it would not have to convert LF to CRLF.  Is there a
practical way of doing this (which would work across any platform)? 
Also, the code above was run under Windows.  I have not yet tested under
*nix yet.

Is this the desired behavior of open-output-file, or is there a function
that is more suitable to what I am trying to do?  Thanks for your help.

Michael Coppola

Posted on the users mailing list.