用utf8-string读写utf8文件(Windows和Linux下都行)
来源:互联网 发布:办公椅三视图带数据 编辑:程序博客网 时间:2024/06/10 01:32
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>
- 用utf8-string读写utf8文件(Windows和Linux下都行)
- java读写UTF8文件
- VB 读写UTF8文件
- UTF8文件读写
- linux下把文件转换为utf8
- 读写INI文件UTF8格式
- Linux下 GB2312和UTF8转换接口
- Linux下 GB2312和UTF8转换接口
- 读写Unicode和UTF8格式文件
- 将windows下编辑好的文件(GBK)转换成Linux下的格式(UTF8)
- Linux下gbk-utf8文件和目录下所有文件转码
- Windows下的UTF8编码
- 在windows下把txt文件改为utf8格式
- linux 下 gb18030 转码成 utf8
- 在windows平台下UTF8和GB232之间的转换
- csv文件utf8编码后windows操作系统(中文GBK环境)下乱码
- UTF8方式读写文件的模块
- linux下找出utf8带BOM头的文件
- msd基数排序
- java基础知识学习笔记2
- [NOI2004]郁闷的出纳员
- 处理多维数组的列
- windows server 2008下安装windows phone 7开发工具的补充
- 用utf8-string读写utf8文件(Windows和Linux下都行)
- shell 移动文件
- postfix extmail 快速搭建
- DirectDraw 示例
- shell 判断键盘输入
- Eclipse svn 安装地址
- 我应走我的路,还是走你的路?
- Fibonacci序列 —— O(log n)求Fibonacci数列(非矩阵法)
- 游戏开发高级技术