用utf8-stri ng讀寫utf8文件 (Windows和Linux下都行)

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">&#x7528;utf8-string&#x8BFB;&#x5199;utf8&#x6587;&#x4EF6;</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">&#x25CF;</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">&#x8BFB;&#x6587;&#x4EF6;&#xFF08;Read&#xFF09;</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">&#x5199;&#x6587;&#x4EF6;&#xFF08;Write&#xFF09;</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>

發佈了39 篇原創文章 · 獲贊 3 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章