gtk2hs在windows下读取utf8文件,显示出来会出现乱码。
根据下面资料使用utf8-string进行utf8文本文件的读写操作,就能解决乱码问题。
注:用Windows自带的记事本新建的utf8文件开头有一个UTF8字符,即三个字节:/239/187/191。
参考:
http://hackage.haskell.org/package/utf8-string
http://www.haskell.org/haskellwiki/UTF-8 http://zincer.blogbus.com/logs/49781044.html
官方示例代码 @ http://www.haskell.org/haskellwiki/UTF-8
import System.IO.UTF8 import Prelude hiding (readFile, writeFile) import System.Environment (getArgs) main :: IO () main = do args <- getArgs mapM_ reverseUTF8File args reverseUTF8File :: FilePath -> IO () reverseUTF8File f = do c <- readFile f writeFile (f ++ ".rev") $ reverseLines c where reverseLines = unlines . map reverse . lines
本人代码:
-- file: main.hs
module Main where
import Graphics.UI.Gtk import Graphics.UI.Gtk.Glade import System.IO.UTF8 import Prelude hiding (readFile, writeFile, putStrLn, print)
main = do initGUI -- load up the glade file dialogXmlM <- xmlNew "gbhs.glade" let dialogXml = case dialogXmlM of (Just dialogXml) -> dialogXml Nothing -> error "can't find the glade file /"simple.glade/" / /in the current directory" -- get a handle on a couple widgets from the glade file window <- xmlGetWidget dialogXml castToWindow "dialog1" button1 <- xmlGetWidget dialogXml castToButton "button1" button2 <- xmlGetWidget dialogXml castToButton "button2" entry <- xmlGetWidget dialogXml castToEntry "entry1" -- do something with the widgets, just to prove it works button1 `onClicked` do text <- readFile "utf8.txt" entrySetText entry text --putStrLn text button2 `onClicked` do text <- entryGetText entry writeFile "utf8.txt" text --putStrLn text window `onDestroy` mainQuit -- show everything widgetShowAll window mainGUI
gbhs.glade文件:
<?xml version="1.0"?> <glade-interface> <!-- interface-requires gtk+ 2.16 --> <!-- interface-naming-policy project-wide --> <widget class="GtkDialog" id="dialog1"> <property name="border_width">5</property> <property name="title" translatable="yes">用utf8-string读写utf8文件</property> <property name="type_hint">normal</property> <property name="has_separator">False</property> <child internal-child="vbox"> <widget class="GtkVBox" id="dialog-vbox1"> <property name="visible">True</property> <property name="orientation">vertical</property> <property name="spacing">2</property> <child> <widget class="GtkEntry" id="entry1"> <property name="visible">True</property> <property name="can_focus">True</property> <property name="invisible_char">●</property> </widget> <packing> <property name="position">1</property> </packing> </child> <child internal-child="action_area"> <widget class="GtkHButtonBox" id="dialog-action_area1"> <property name="visible">True</property> <property name="layout_style">end</property> <child> <widget class="GtkButton" id="button1"> <property name="label" translatable="yes">读文件(Read)</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> </widget> <packing> <property name="expand">False</property> <property name="fill">False</property> <property name="position">0</property> </packing> </child> <child> <widget class="GtkButton" id="button2"> <property name="label" translatable="yes">写文件(Write)</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> </widget> <packing> <property name="expand">False</property> <property name="fill">False</property> <property name="position">1</property> </packing> </child> </widget> <packing> <property name="expand">False</property> <property name="pack_type">end</property> <property name="position">0</property> </packing> </child> </widget> </child> </widget> </glade-interface>