Add line to existing text file in IronScheme

We are trying to create a log file using IronScheme, and we wrote code for it using racket. It works great in a racket, but IronScheme throws an error. This is what we have so far:

(define write-to-log (lambda(whatToWrite) (with-output-to-file "robot-log.txt" (lambda () (printf (string-append whatToWrite "\r\n" ))) #:exists 'append))) 

See how we use the optional parameter "exists" when using with-output-to-file. We do not know how to make this optional parameter work with IronScheme. Are there any ways to make this work or alternative methods?

Note that we want to add a line to an existing .txt file. If we do not use the optional argument, an error occurs saying that the file already exists.

+3
source share
2 answers

IronScheme supports R6RS :)

file-options not available in with-output-to-file , so you need to use open-file-output-port .

Example (incorrect):

 (let ((p (open-file-output-port "robot-log.txt" (file-options no-create)))) (fprintf p "~a\r\n" whatToWrite) (close-port p)) 

Update:

The above function is not . You seem to have found a bug in IronScheme. It is unclear though from R6RS that file-options should behave like append, if any. Once again I will research and provide feedback.

Update 2:

I talked to one of the R6RS editors and it has no portable way to specify "add mode". Of course, we have it available in .NET, so I will fix the problem by adding another file-options to add. I will also think about adding some overloads for the "plain io" routines to handle this, since using the above code is pretty tedious. Thank you for identifying the problem!

Update 3:

I reviewed this question. From TFS rev 114008, append added to file-options . In addition, with-output-to-file , call-with-output-file and open-output-file have an additional optional parameter specifying 'append-mode'. You can get the latest build from http://build.ironscheme.net/ .

Example:

 (with-output-to-file "test.txt" (lambda () (displayln "world")) #t) 
+1
source

I understand that IronScheme is based on R5RS. From the R5RS Documentation :

for with-output-to-file , no effect is specified if the file already exists.

Therefore, throwing an error certainly matches the specification, and portability of Racket code should not be expected.

Caution: this code was run in another R5RS implementation, not IronScheme.

If you just want to add to an existing file in R5RS:

 (define my-file (open-output-file "robotlog.txt")) (display (string-append what-to-write "\r\n") my-file) (close-output-port my-file) 

This is a simple approach that can bring you closer to what you want.

0
source

All Articles