试试看这个方法行不行 private sub command1_click() dim varOutput As Double varOutput = Val(Text1.Text) ' 假设你要保存得东西在textbox中 on Error GoTo FileError Open "c:\a.dat" For Binary As #1 Put #1,,varOutput Close #1 Exit Sub FileError: MsgBox "notice!!" End Sub
TO:enmity(灵感之源) 我已给你发邮件了,为什么不回复!
我就是声明的Byte数组,但是文件长度仍不对,为什么?!
问题已解决,谢谢各位,特别要谢谢enmity(灵感之源),主要问题下面代码中加下划线的三行代码,不仅正确,而且比我用一个字节一个字节保存的方法效率高了很多倍(以前简直太慢了),但是我想问一下高手,这是为什么,原因呢? bytFileBuffer() = getFieldValue(strName) Put #1, 1, bytFileBuffer() 这两句以前还从没用过,绝!!!结果如下: ---------------------------- Public Function savetoFile(strName As String, strFilePath As String, strFileName As String, blnOverWrite As Boolean) As Boolean '把strName项的值保存到文件strFileName中,并说明是否覆盖存在的文件 '要求strFielPath是带盘符的绝对路径,最后以\结尾 Dim RPath As String Dim filesize As Long Dim fs As Scripting.FileSystemObject
Dim bytFileBuffer() As Byte '--------------------------------------------- ' dim FileValue as variant On Error GoTo error_handle
bytFileBuffer() = getFieldValue(strName) '-------------------------------------------------- ' filevalue=getFieldValue(strName) Set fs = New Scripting.FileSystemObject
If (fs.FileExists(RPath) And blnOverWrite = False) Then '检查是否有相同的文件名的文件存在 strLastError = "文件已经存在!" Err.Raise 1 End If
Open RPath For Binary As #1 Put #1, 1, bytFileBuffer() '------------------------------------------------ ' Put #1, , FileValue Close #1 savetoFile = True
Exit Function error_handle: strLastError = "文件保存失败" savetoFile = False End Function
原文件:2.05 KB (2,104 字节)
保存后的文件:文件名:logo.jpg(1992字节) 我好像听说在使用put时他会进行什么格式转换,在我的程序中可以看到lenb(fileValue)==2104,可是保存后就是不对。有时会在文件开头加上一点,有时会在结尾多一点,少一点,反正看方法的不同,就是不正确!
[email protected]
用cbyte(值)强制转换格式例:
put 1,,cbyte(data)
可在fileValue中已经是二进制的啦,而且,fileValue也是Variant我最想知道的是不论是用Scripting.FileSystemObject还是put的老方法,它是如果处理我的数据的,这个过程到底怎么回事,我是否可以施加控制。
比如说:Vaiant是不是和String(BSTR)一样的存在内存中,程序是从哪一个字节开始读数据,如何。高手就说说嘛!大家共同学习!
private sub command1_click()
private sub command1_click()
dim varOutput As Double
varOutput = Val(Text1.Text) ' 假设你要保存得东西在textbox中
on Error GoTo FileError
Open "c:\a.dat" For Binary As #1
Put #1,,varOutput
Close #1
Exit Sub
FileError:
MsgBox "notice!!"
End Sub
我已给你发邮件了,为什么不回复!
这两句以前还从没用过,绝!!!结果如下:
----------------------------
Public Function savetoFile(strName As String, strFilePath As String, strFileName As String, blnOverWrite As Boolean) As Boolean
'把strName项的值保存到文件strFileName中,并说明是否覆盖存在的文件
'要求strFielPath是带盘符的绝对路径,最后以\结尾
Dim RPath As String
Dim filesize As Long
Dim fs As Scripting.FileSystemObject
Dim bytFileBuffer() As Byte
'---------------------------------------------
' dim FileValue as variant On Error GoTo error_handle
filesize = getFieldSize(strName)
RPath = strFilePath & strFileName '包含文件名的文件路径
ReDim bytFileBuffer(filesize)
bytFileBuffer() = getFieldValue(strName)
'--------------------------------------------------
' filevalue=getFieldValue(strName)
Set fs = New Scripting.FileSystemObject
If (fs.FileExists(RPath) And blnOverWrite = False) Then '检查是否有相同的文件名的文件存在
strLastError = "文件已经存在!"
Err.Raise 1
End If
Open RPath For Binary As #1
Put #1, 1, bytFileBuffer()
'------------------------------------------------
' Put #1, , FileValue
Close #1
savetoFile = True
Exit Function
error_handle:
strLastError = "文件保存失败"
savetoFile = False
End Function
再大就不可以了。