明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 392|回复: 2

[源码] 读一个txt文件(UTF-8),分行读入,不乱码。和判断UTF-8编码

[复制链接]
发表于 3 天前 | 显示全部楼层 |阅读模式
本帖最后由 null. 于 2025-5-11 15:07 编辑

解决读一个txt文件(UTF-8),分行读入,不乱码。
e:\\123.txt
设置为二进制模式
设置3为二进制模式
设置为4二进制模式


  1. (defun read-utf8-file (sm / binarystream file filestream lines pos size str)
  2.   (setq file "e:\\123.txt")
  3.   (setq binarystream (vlax-create-object "Adodb.Stream"))
  4.   (vlax-invoke binarystream 'Open)
  5.   (vlax-invoke-Method binarystream 'LoadFromFile file)
  6.   
  7.   (vlax-put-property binarystream 'Type 2) ; 2文本模式读取
  8.   (vlax-put-property binarystream 'Charset "utf-8")
  9.   (vlax-put-property binarystream 'Position 2);将位置重置为起始位置
  10.   (setq str (Vlax-Invoke-Method binarystream 'ReadText nil))
  11.   (vlax-invoke binarystream 'flush)
  12.   (vlax-invoke binarystream 'Close)   
  13.   (vlax-release-object binarystream)
  14.   (while (setq pos (vl-string-search "\r\n" str))
  15.     (setq lines (cons (substr str 1 pos) lines))
  16.     (setq str (substr str (+ pos 3)))
  17.   )
  18.   (reverse (cons str lines))
  19. )






  • (read-utf8-file nil)
    ("设置为二进制模式" "设置3为二进制模式" "设置为4二进制模式")

评分

参与人数 1明经币 +1 金钱 +5 收起 理由
tigcat + 1 + 5 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 前天 15:05 | 显示全部楼层
加一个判断TXT是否为UTF-8
  1. (defun c:CheckUTF8 (/ binarystream bomFlag byte1 byte2 byte3 check_continuation_byte fHandle filePath str1 str2 utf8Flag)
  2.   (defun check_continuation_byte ( dd / byte)
  3.     (setq byte (car str2))
  4.     (setq str2 (cdr str2))
  5.     (if (and byte (<= 128 byte 191)) t nil)
  6.   )
  7.   (setq filePath (getfiled "选择TXT文件" "" "txt" 16))
  8.   (if filePath
  9.     (progn
  10.       (setq binarystream (vlax-create-object "Adodb.Stream"))
  11.       (vlax-put-property binarystream 'Type 1)
  12.       (vlax-put-property binarystream 'Mode 3)
  13.       (vlax-invoke binarystream 'Open)
  14.       (vlax-put-property binarystream 'Position 0)
  15.       (vlax-invoke-Method binarystream 'LoadFromFile filePath)  
  16.       (setq str1 (Vlax-Invoke-Method binarystream 'Read nil))
  17.       ;(setq str1 (Vlax-Invoke-Method binarystream 'Read 3))      
  18.       (vlax-invoke binarystream 'flush)
  19.       (vlax-invoke binarystream 'Close)
  20.       (vlax-release-object binarystream)
  21.       (setq str2 (vlax-safearray->list (vlax-variant-value str1) ))
  22.       ;(setq fHandle (open filePath "rb")) ; 二进制方式打开文件
  23.       (setq bomFlag nil
  24.         utf8Flag t)
  25.       
  26.       ; 阶段一:BOM头判断 [1]()
  27.       (setq byte1 (nth 0 str2)
  28.         byte2 (nth 1 str2)
  29.         byte3 (nth 2 str2))
  30.       (if (and (= byte1 239) (= byte2 187) (= byte3 191))
  31.         (setq bomFlag t)
  32.         (progn
  33.           ; 无BOM时回退到起始位置
  34.          
  35.           (setq str2 (vlax-safearray->list (vlax-variant-value str1) ))
  36.         )
  37.       )
  38.       
  39.       ; 阶段二:UTF-8编码规则验证 [5]()
  40.       (while (and utf8Flag (setq byte1 (car str2)))
  41.         (setq str2 (cdr str2))
  42.         (cond
  43.           ((< byte1 128) ; 单字节字符(0xxxxxxx)
  44.           )
  45.           ((<= 194 byte1 222) ; 双字节字符(110xxxxx)
  46.             (if (null (check_continuation_byte fHandle))
  47.               (setq utf8Flag nil)
  48.             )
  49.           )
  50.           ((<= 224 byte1 239) ; 三字节字符(1110xxxx)
  51.             (if (or (null (check_continuation_byte fHandle))
  52.                   (null (check_continuation_byte fHandle)))
  53.               (setq utf8Flag nil)
  54.             )
  55.           )
  56.           ((<= 240 byte1 244) ; 四字节字符(11110xxx)
  57.             (if (or (null (check_continuation_byte fHandle))
  58.                   (null (check_continuation_byte fHandle))
  59.                   (null (check_continuation_byte fHandle)))
  60.               (setq utf8Flag nil)
  61.             )
  62.           )
  63.           (t (setq utf8Flag nil))
  64.         )
  65.       )
  66.       
  67.       
  68.       ; 输出结果
  69.       (alert
  70.         (strcat "文件编码:"
  71.           (cond
  72.             (bomFlag "UTF-8 with BOM")
  73.             (utf8Flag "UTF-8 without BOM")
  74.             (t "非UTF-8编码(可能是ANSI/GBK)")
  75.           )
  76.         )
  77.       )
  78.     )
  79.   )
  80.   (princ)
  81. )


回复 支持 反对

使用道具 举报

 楼主| 发表于 前天 15:20 | 显示全部楼层
本帖最后由 null. 于 2025-5-11 23:20 编辑


可以自动判断TXT格式,读取TXT内容。(仅区分 UTF-8和UTF-8 BOM和ANSI)




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 1明经币 +1 收起 理由
自贡黄明儒 + 1 很给力!

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-5-13 15:08 , Processed in 0.149275 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表