作了一个窗体,上面有文本框、组合框等,用于存放一些相关初始设置。我想将这些信息保存到一二进制文件中,并且每行存放一个控件内容,如第一行存放Text1的内容,第二行存放Text2的内容等等。请问我该怎么样编写代码。怎么我用下面的代码写成的二进制文件不是我要实现的效果呢?
Open strFile For Binary As #1
Put #1,, Trim(Text1.Text)
Put #1,, Trim(Text2.Text)
...
Close #1
谢谢!
Open strFile For Binary As #1
Put #1,, Trim(Text1.Text)
Put #1,, Trim(Text2.Text)
...
Close #1
谢谢!
Put #1,, Trim(Text1.Text) & vbcrlf
Put #1,, Trim(Text2.Text) & vbcrlf
...
Close #1
Put #1,, Trim(Text2.Text) & chr(13)& chr(10)
//其实你最好还是用ini文件来实现比较好!
VB的String是Unicode编码,写入文件是ANSI编码,写入时要进行转换,后者字节数肯定比前者少,用二进制方式写入时为保证字节数对齐,VB用自动用Chr(0)补齐。
所以二进制方式写文本很麻烦的,需先在内存中做字节数组转换,估计你不会,所以建议还是回归基础,换为OutPut或Append方式,写文本。
dim a() as byte,s as string
s="dfg枯理jkk"
a=strconv(s,vbfromunicode)
不过,我还是想说,真的不用这样,写入位置控制起来,很麻烦,换为OutPut或Append方式打开文件,以上的事,VB就帮你做了。
'================================
'(来源于)http://search.csdn.net/Expert/topic/498/498535.xml?temp=.6198084
'================================' 该函数获得一个字符的高字节或低字节
Function GetCharByte(ByVal OneChar As Integer, ByVal IsHighByte As Boolean) As Byte
If IsHighByte Then
If OneChar >= 0 Then
GetCharByte = CByte(OneChar \ 256)
'右移8位,得到高字节
Else
GetCharByte = CByte((OneChar And &H7FFF) \ 256) Or &H80
End If
Exit Function
Else
GetCharByte = CByte(OneChar And &HFF)
'屏蔽掉高字节,得到低字节
Exit Function
End If
End FunctionSub StrToByte(StrToChange As String, ByteArray() As Byte)
'该函数将一个字符串转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, length As Integer
Dim OneChar As Integer count = 0
length = Len(StrToChange)
LowBound = LBound(ByteArray)
UpBound = UBound(ByteArray) For i = LowBound To UpBound
ByteArray(i) = 0 '初始化字节数组
Next For i = LowBound To UpBound
count = count + 1
If count <= length Then
OneChar = Asc(Mid(StrToChange, count, 1)) If (OneChar > 255) Or (OneChar < 0) Then
'该字符是非ASCII字符
ByteArray(i) = GetCharByte(OneChar, True) '得到高字节
i = i + 1
If i <= UpBound Then ByteArray(i) = GetCharByte(OneChar, False)
'得到低字节
Else
'该字符是ASCII字符
ByteArray(i) = OneChar
End If
Else
Exit For
End If
Next
End SubSub ChangeStrAryToByte(StrAry() As String, ByteAry() As Byte)
'将字符串数组转换成字节数组
Dim LowBound, UpBound As Integer
Dim i, count, StartPos, MaxLen As Integer
Dim TmpByte() As Byte LowBound = LBound(StrAry)
UpBound = UBound(StrAry)
count = 0
ReDim ByteAry(0) For i = LowBound To UpBound
MaxLen = LenB(StrAry(i))
ReDim TmpByte(MaxLen + 1)
ReDim Preserve ByteAry(count + MaxLen + 1)
Call StrToByte(StrAry(i), TmpByte) '转换一个字符串
StartPos = count
Do
ByteAry(count) = TmpByte(count - StartPos)
count = count + 1
If ByteAry(count - 1) = 0 Then Exit Do
Loop '将每一个字符串对应的字节数组按顺序填入结果数组中
ReDim Preserve ByteAry(count - 1)
Next i
End Sub'将二进制转换成字符
'方法一
……
strGet = StrConv(bytArray(), vbUnicode) '将二进制转化为string
strGet = Replace(strGet, Chr(0), " ")
……
'方法二
'(来源于)http://search.csdn.net/Expert/topic/119/119739.xml?temp=.2302057
……
Dim i As Integer, intUpIndex As Integer, lngTemp As Long
Dim strTemp As StringstrGet = "": intUpIndex = UBound(bytArray)
For i = 0 To intUpIndex - 1
strTemp = "": intTemp = 0
If bytArray(i) = 0 Then bytArray(i) = 32 '空格
If bytArray(i) >= 32 And bytArray(i) <= 126 Then
strTemp = Chr(bytArray(i))
ElseIf bytArray(i) = 13 Then
strTemp = Chr(bytArray(i))
Else
lngTemp = bytArray(i)
i = i + 1
lngTemp = lngTemp * 256 + bytArray(i)
strTemp = Chr(lngTemp)
End If
strGet = strGet & strTemp
Next i
……