为什么读取第一个字符是乱码,帮帮小弟
小弟我刚刚学习VB,要读取文本文件的内容为"SIM2@@@@@"Dim StrTmp as String
Open c:\test.txt For Input As #1
While Not EOF(1)
StrTmp = StrTmp & Input(1, #1)
Wend
Close #1
调试的时候,第一个字符读不出来,结果是StrTmp=" "之后的第二个字符以后竟然又能读取了,结果是
StrTmp=" IM2@@@@"
将StrTmp的内容写入文本文件后文本文件全部都是乱码了
这是怎么回事呢? --------------------编程问答-------------------- 估计是含有非法字符,将文件存为Unicode格式,再用line input 读取试试,
不知道你说的第一个字符是什么,实在不行将这个非法字符删除 --------------------编程问答--------------------
Dim StrTmp as String
Open "c:\test.txt" For binary As #1
strtmp=space(lof(1))
get #1,,strtmp
Close #1
msgbox strtmp
试下. --------------------编程问答--------------------
Option Explicit
Dim strFile As String
Dim strMemo As String
Dim intFileNum As Integer
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
'过程功能:
'功能描述:Input方式读入文件
'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Private Sub btnInput_Click()
On Error GoTo errSub
strMemo = ""
intFileNum = FreeFile(0)
Open strFile For Input As #intFileNum
Input #intFileNum, strMemo
Close #intFileNum
txtMemo.Text = strMemo
Exit Sub
errSub:
End Sub
Private Sub Form_Load()
strFile = App.Path & "\111.txt"
End Sub
通过测试 --------------------编程问答-------------------- 楼主的问题应该就是因为 你的 .txt 文件是 Unicode格式 引起的。
把它保存为 ANSI 编码格式就可以了。
--------------------编程问答-------------------- 文本文件另存为ANSI 或者读Unicode文件:
Dim retStr As String
Dim buf() As Byte
Dim iLen As Long
iLen = FileLen("d:\a.txt")
ReDim buf(iLen)
Open "d:\a.txt" For Binary As #1
Get #1, , buf
Close #1
retStr = StrConv(buf, vbNarrow) --------------------编程问答-------------------- 你是想读完里面的数据么,可以一次性读完的。strtmp=input$(lof(1),1) 如果只是一行 line input #1,strtmp
补充:VB , 基础类