On Monday, October 5, 2015 at 1:07:31 AM UTC+2, Josh English wrote:
> I am trying to learn Racket by creating a Todo manager based on the Todo.txt 
> format by Gina Trapani (http://todotxt.com/)
> 
> I also have an Android tablet that uses a todo.txt application. This 
> application uses "\n\r" at the end of each line. I'm on Windows 7, so I need 
> this newline to work as well, because if I open the file up in Notepad, it 
> messes up my lines and my todos (normally one per line) become one multi-line 
> todo.
> 
> The program reads the text file using file->lines and filters any blank lines.
> 
> The program writes the text file and I manually write the "\n\r" using write, 
> display, and print. That is to say, I've tried all three and ended up with 
> extra "\n" characters.
> 
> Here is a snippet from my do-task procedure:
> 
>  (with-output-to-file (task-file-path) #:mode 'text #:exists 'truncate/replace
>     (lambda ()
>     (write (string-join (drop-right uptos 1) "\n\r"))
>     (write "\n\r")
>     (write (complete-task (last uptos) (rest taskstuff)))
>     (write "\n\r")
>     (write (string-join afters "\n\r")))))  
> 
> When I open the file with SciTe, it shows the CR and LF characters at the end 
> of each line, plus a blank line with a CR character.
> 
> The complite-task procedure transforms a task and returns a trimmed string.
> 
> Where is this extra blank line coming from?
> 
> Full draft code at http://pastebin.com/z3C5BQJP
> 
> Thanks,
> 
> Josh

It's from #:mode 'text, that translates \n to \r\n: this way, programs can end 
the line with \n on all platforms, yet produce the right end-of-line on 
Windows. This behavior is copied from the C libraries. BTW, are you sure you 
want \n\r? That means LF CR, while on Windows you want \r\n.

To sum up: You can either use #:mode 'text and just use \n (which will produce 
native end-of-lines, hence will produce the format you want only on Windows) 
*or* specify #:mode 'binary and use \r\n (not \n\r), which will produce the 
same result on whatever platform.

Sources:
1. Following with-output-to-file docs shows that open-output-file
http://docs.racket-lang.org/reference/file-ports.html?q=open-output-file%09#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._open-output-file%29%29
2. For background on escape sequences and behavior on Windows, one starting 
point is here:
https://en.wikipedia.org/wiki/Escape_sequences_in_C#Table_of_escape_sequences
For the equivalent of the behavior above, see docs for fopen on Windows, in 
particular text and binary modes.

Cheers,
Paolo

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to