求救 读取文本文件中的中文出现乱码,写操作失败

gooore 2011-05-12 11:31:42
您好,我的文本文件中包含中文字符,使用NotePad++查看编码格式为UTF-8 without BOM,

我需要把文本读出来,然后替换里面的某些字符,然后再写回去。我使用的是下面的读写函数,中间用Replace函数替换字符。

Public Sub ReadAll(sTempFile As String, sAll As String)
If sTempFile = "" Then
Exit Sub
End If
Dim lFreeHandle As Long
lFreeHandle = FreeFile

Open sTempFile For Input As #lFreeHandle
sAll = Input(LOF(lFreeHandle), lFreeHandle)
Close #lFreeHandle

End Sub

Public Sub WriteAll(sTempFile As String, sAll As String)
If sTempFile = "" Then
Exit Sub
End If
Dim lFreeHandle As Long
lFreeHandle = FreeFile
Open sTempFile For Output As #lFreeHandle
Print #lFreeHandle, sAll
Close #lFreeHandle
End Sub

我的问题是现在读出来的中文是乱码,可能是宽字符的原因,替换字符后,写回去文件面目全非。

请问您,非ANSI编码的文件该怎么读些最好,最好附上您的解决反案。

我在网上搜索了一些比如使用Richtextbox,也出现一样的问题。

相当郁闷,无助中。

非常感谢您的帮助
...全文
174 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
咸清 2011-05-12
  • 打赏
  • 举报
回复
vb 不是.net,不直接支持UTF8
所以你还要转化一下
gooore 2011-05-12
  • 打赏
  • 举报
回复
是的

[Quote=引用 2 楼 simon__sun 的回复:]
Richtextbox 打开文件就是乱码吗
[/Quote]
simon__sun 2011-05-12
  • 打赏
  • 举报
回复
Richtextbox 打开文件就是乱码吗
dbcontrols 2011-05-12
  • 打赏
  • 举报
回复
用下面这个函数转一下试试
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim lRet As Long
Dim lLength As Long
Dim lBufferSize As Long
lLength = UBound(Utf) - LBound(Utf) + 1
If lLength <= 0 Then Exit Function
lBufferSize = lLength * 2
Utf8ToUnicode = String$(lBufferSize, Chr(0))
lRet = MultiByteToWideChar(CP_UTF8, 0, VarPtr(Utf(0)), lLength, StrPtr(Utf8ToUnicode), lBufferSize)
If lRet <> 0 Then
Utf8ToUnicode = Left(Utf8ToUnicode, lRet)
Else
Utf8ToUnicode = ""
End If
End Function
VBAdvisor 2011-05-12
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 vbadvisor 的回复:]

VB code

Public Function UnicodeFile_Read_VB(ByVal sFileName As String, _
Optional ByVal bRemoveBOM As Boolean) As String
Dim FF As Long
Dim b() As Byte
Dim s As String
Const uBOM ……
[/Quote]
会多出一个?,这是因为Unicode的Text文件前4位字节是&HFEFF(UTF16LE)或&HFFFE(UTF16BE),对于UTF-8,前4位字节是&HBFBBEF。上面的UnicodeFile_Read_VB是读Unicode文件,而不是utf-8.
VBAdvisor 2011-05-12
  • 打赏
  • 举报
回复

Public Function UnicodeFile_Read_VB(ByVal sFileName As String, _
Optional ByVal bRemoveBOM As Boolean) As String
Dim FF As Long
Dim b() As Byte
Dim s As String
Const uBOM As String = "ÿþ"
On Error Resume Next
FF = FreeFile
Open sFileName For Binary Access Read As FF
ReDim b(LOF(FF))
Get FF, , b
Close FF
s = b
If bRemoveBOM Then
If InStr(s, uBOM) = 1 Then
s = Replace$(s, uBOM, "")
End If
End If
b=s
UnicodeFile_Read_VB = FromMultiByteb(b, 65001)
End Function

VBAdvisor 2011-05-12
  • 打赏
  • 举报
回复
Public Function FromMultiByteb(ByRef b() As Byte, ByVal CodePage As Long) As String
Dim lngOutLen As Long
Dim strWide As String

lngOutLen = MultiByteToWideChar(CodePage, _
0&, _
VarPtr(b(LBound(b))), _
UBound(b) - LBound(b) + 1, _
0, _
0)

If lngOutLen = 0 Then
Err.Raise vbObjectError Or &HC312&, _
"modHelpers.FromMultiByteb", _
"Failed to decode string, system error " _
& CStr(Err.LastDllError)
Else
strWide = String$(lngOutLen, 0)
lngOutLen = MultiByteToWideChar(CodePage, _
0&, _
VarPtr(b(LBound(b))), _
UBound(b) - LBound(b) + 1, _
StrPtr(strWide), _
lngOutLen)
If lngOutLen = 0 Then
Err.Raise vbObjectError Or &HC312&, _
"modHelpers.FromMultiByteb", _
"Failed to decode string, system error " _
& CStr(Err.LastDllError)
Else
FromMultiByteb = strWide
End If
End If
End Function
sixoxen 2011-05-12
  • 打赏
  • 举报
回复
首先要搞清楚这是什么乱码,是怎么导致的乱码,然后才好想办法解决,你可以把乱码发上来看看,也可以用“解字板”试试: http://www.xdowns.com/soft/softdown.asp?softid=52984
也可参考下我这篇小文:http://blog.csdn.net/sixoxen/archive/2011/05/11/6412556.aspx
gooore 2011-05-12
  • 打赏
  • 举报
回复
不知道啊,也不知道怎么换.

[Quote=引用 6 楼 simon__sun 的回复:]
能不能用Binary 的方式打开替换
[/Quote]
simon__sun 2011-05-12
  • 打赏
  • 举报
回复
能不能用Binary 的方式打开替换
gooore 2011-05-12
  • 打赏
  • 举报
回复
多谢回复

我在使用API函数MultiByteToWideChar时直接跳出,程序中断了.


Dim Temp() As Byte
Dim Templen As Long
Dim I As Long
Templen = FileLen(sFilePath1)
ReDim Temp(Templen) As Byte
Open sFilePath1 For Binary As #1
Get #1, , Temp
Close #1
sFileContent = Utf8ToUnicode(Temp)






[Quote=引用 1 楼 dbcontrols 的回复:]
用下面这个函数转一下试试

VB code
Function Utf8ToUnicode(ByRef Utf() As Byte) As String
Dim lRet As Long
Dim lLength As Long
Dim lBufferSize As Long
lLength = UBound(Utf) - LBound(Utf) + 1
If lLength <= 0……
[/Quote]

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧