用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

相关文章

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

linux下多个硬盘划分到同一挂载点问题

《linux下多个硬盘划分到同一挂载点问题》在Linux系统中,将多个硬盘划分到同一挂载点需要通过逻辑卷管理(LVM)来实现,首先,需要将物理存储设备(如硬盘分区)创建为物理卷,然后,将这些物理卷组成... 目录linux下多个硬盘划分到同一挂载点需要明确的几个概念硬盘插上默认的是非lvm总结Linux下多

linux进程D状态的解决思路分享

《linux进程D状态的解决思路分享》在Linux系统中,进程在内核模式下等待I/O完成时会进入不间断睡眠状态(D状态),这种状态下,进程无法通过普通方式被杀死,本文通过实验模拟了这种状态,并分析了如... 目录1. 问题描述2. 问题分析3. 实验模拟3.1 使用losetup创建一个卷作为pv的磁盘3.

Windows设置nginx启动端口的方法

《Windows设置nginx启动端口的方法》在服务器配置与开发过程中,nginx作为一款高效的HTTP和反向代理服务器,被广泛应用,而在Windows系统中,合理设置nginx的启动端口,是确保其正... 目录一、为什么要设置 nginx 启动端口二、设置步骤三、常见问题及解决一、为什么要设置 nginx

Linux环境变量&&进程地址空间详解

《Linux环境变量&&进程地址空间详解》本文介绍了Linux环境变量、命令行参数、进程地址空间以及Linux内核进程调度队列的相关知识,环境变量是系统运行环境的参数,命令行参数用于传递给程序的参数,... 目录一、初步认识环境变量1.1常见的环境变量1.2环境变量的基本概念二、命令行参数2.1通过命令编程

Linux之进程状态&&进程优先级详解

《Linux之进程状态&&进程优先级详解》文章介绍了操作系统中进程的状态,包括运行状态、阻塞状态和挂起状态,并详细解释了Linux下进程的具体状态及其管理,此外,文章还讨论了进程的优先级、查看和修改进... 目录一、操作系统的进程状态1.1运行状态1.2阻塞状态1.3挂起二、linux下具体的状态三、进程的

Linux编译器--gcc/g++使用方式

《Linux编译器--gcc/g++使用方式》文章主要介绍了C/C++程序的编译过程,包括预编译、编译、汇编和链接四个阶段,并详细解释了每个阶段的作用和具体操作,同时,还介绍了调试和发布版本的概念... 目录一、预编译指令1.1预处理功能1.2指令1.3问题扩展二、编译(生成汇编)三、汇编(生成二进制机器语

Rsnapshot怎么用? 基于Rsync的强大Linux备份工具使用指南

《Rsnapshot怎么用?基于Rsync的强大Linux备份工具使用指南》Rsnapshot不仅可以备份本地文件,还能通过SSH备份远程文件,接下来详细介绍如何安装、配置和使用Rsnaps... Rsnapshot 是一款开源的文件系统快照工具。它结合了 Rsync 和 SSH 的能力,可以帮助你在 li

Linux部署jar包过程

《Linux部署jar包过程》文章介绍了在Linux系统上部署Java(jar)包时需要注意的几个关键点,包括统一JDK版本、添加打包插件、修改数据库密码以及正确执行jar包的方法... 目录linux部署jar包1.统一jdk版本2.打包插件依赖3.修改密码4.执行jar包总结Linux部署jar包部署

在 Windows 上安装 DeepSeek 的完整指南(最新推荐)

《在Windows上安装DeepSeek的完整指南(最新推荐)》在Windows上安装DeepSeek的完整指南,包括下载和安装Ollama、下载DeepSeekRXNUMX模型、运行Deep... 目录在www.chinasem.cn Windows 上安装 DeepSeek 的完整指南步骤 1:下载并安装