RE: [racket-users] Re: Writing text to a file with correct newlines
Yes, notepad is strange. Why not use wordpad? Jos -Original Message- From: racket-users@googlegroups.com [mailto:racket-users@googlegroups.com] On Behalf Of Josh English Sent: martes, 06 de octubre de 2015 6:06 To: Racket Users Subject: [racket-users] Re: Writing text to a file with correct newlines On Monday, October 5, 2015 at 2:04:16 AM UTC-7, Paolo Giarrusso wrote: > 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_sequence s > For the equivalent of the behavior above, see docs for fopen on Windows, in particular text and binary modes. > > Cheers, > Paolo Paolo, Thanks for the information. It seems that the Android App handles a plain '\n' newline just fine. It's only when I read the file in Notepad that things get strange. -- 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. -- 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.
[racket-users] Re: Writing text to a file with correct newlines
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.
[racket-users] Re: Writing text to a file with correct newlines
On Monday, October 5, 2015 at 11:04:16 AM UTC+2, Paolo Giarrusso wrote: > 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. Ah, I've just realized you say you've tried all three, sorry for missing that. > 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. To be clear: if the requirements you describe are accurate, I'd advise the second way for robustness. But in 99% of racket programs (and, in fact, in most languages), it's most common (though not the most robust) to just use \n and open files in text mode, and produce whatever line ending is the right one on the platform. (Many apps, including good text editors for programmers, should refrain from that though.) Cheers, Paolo > 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.
[racket-users] Re: Writing text to a file with correct newlines
On Monday, October 5, 2015 at 2:04:16 AM UTC-7, Paolo Giarrusso wrote: > 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 Paolo, Thanks for the information. It seems that the Android App handles a plain '\n' newline just fine. It's only when I read the file in Notepad that things get strange. -- 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.