用utf8-stri​ng读写utf8文件​(Windows和Linux下都行)

2024-03-11 08:38

本文主要是介绍用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>

这篇关于用utf8-stri​ng读写utf8文件​(Windows和Linux下都行)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/797276

相关文章

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

Linux进程CPU绑定优化与实践过程

《Linux进程CPU绑定优化与实践过程》Linux支持进程绑定至特定CPU核心,通过sched_setaffinity系统调用和taskset工具实现,优化缓存效率与上下文切换,提升多核计算性能,适... 目录1. 多核处理器及并行计算概念1.1 多核处理器架构概述1.2 并行计算的含义及重要性1.3 并

Linux线程之线程的创建、属性、回收、退出、取消方式

《Linux线程之线程的创建、属性、回收、退出、取消方式》文章总结了线程管理核心知识:线程号唯一、创建方式、属性设置(如分离状态与栈大小)、回收机制(join/detach)、退出方法(返回/pthr... 目录1. 线程号2. 线程的创建3. 线程属性4. 线程的回收5. 线程的退出6. 线程的取消7.

Linux下进程的CPU配置与线程绑定过程

《Linux下进程的CPU配置与线程绑定过程》本文介绍Linux系统中基于进程和线程的CPU配置方法,通过taskset命令和pthread库调整亲和力,将进程/线程绑定到特定CPU核心以优化资源分配... 目录1 基于进程的CPU配置1.1 对CPU亲和力的配置1.2 绑定进程到指定CPU核上运行2 基于

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

linux解压缩 xxx.jar文件进行内部操作过程

《linux解压缩xxx.jar文件进行内部操作过程》:本文主要介绍linux解压缩xxx.jar文件进行内部操作,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、解压文件二、压缩文件总结一、解压文件1、把 xxx.jar 文件放在服务器上,并进入当前目录#

Linux系统性能检测命令详解

《Linux系统性能检测命令详解》本文介绍了Linux系统常用的监控命令(如top、vmstat、iostat、htop等)及其参数功能,涵盖进程状态、内存使用、磁盘I/O、系统负载等多维度资源监控,... 目录toppsuptimevmstatIOStatiotopslabtophtopdstatnmon

C#读写文本文件的多种方式详解

《C#读写文本文件的多种方式详解》这篇文章主要为大家详细介绍了C#中各种常用的文件读写方式,包括文本文件,二进制文件、CSV文件、JSON文件等,有需要的小伙伴可以参考一下... 目录一、文本文件读写1. 使用 File 类的静态方法2. 使用 StreamReader 和 StreamWriter二、二进