<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 29, 2016 at 11:20 AM, Dennis Raddle <span dir="ltr"><<a href="mailto:dennis.raddle@gmail.com" target="_blank">dennis.raddle@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello, I wrote a program that reads filenames using System.Directory "listDirectory", does some tests on them, and writes certain file names (as read into a FilePath variable by listDirectory) to a text file, which I later parse with Parsec.  I am getting an error on writing to the text file:<div><br></div><div>commitBuffer: invalid argument (invalid character)</div><div><br></div><div>What can I do about this? If I am reading some kind of unusual characters from the FilePath, I want to choose some way of writing them so that the resulting file can still be parsed by Text.Parsec.ByteString. I hope that I can still rely on the "space" parser to find CR and LF, and don't want any other surprises. </div><span class="HOEnZb"><font color="#888888"><div><br></div><div>D</div><div><br></div></font></span></div>
</blockquote></div><br></div><div class="gmail_extra">I'd recommend being explicit about the character encoding you're using, and using the bytestring API for the I/O itself. As an example:</div><div class="gmail_extra"><br></div><div class="gmail_extra">import qualified Data.Text.Lazy as TL</div><div class="gmail_extra">import qualified Data.Text.Lazy.Encoding as TL</div><div class="gmail_extra">import qualified Data.ByteString.Lazy as L</div><div class="gmail_extra"><br></div><div class="gmail_extra">writeFileUtf8 :: FilePath -> String -> IO ()</div><div class="gmail_extra">writeFileUtf8 fp str = L.writeFile fp (TL.encodeUtf8 (TL.pack str))</div><div class="gmail_extra"><br></div><div class="gmail_extra">The default handling of character encodings is reliant on environment variables, which IME makes the textual file writing functions notoriously fragile.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Michael</div></div>