只爱陌生人 2006-10-20 13:53
关于文件读写(为何与原文件的大小不同)
[code]
Option Explicit
Private Sub Command1_Click()
Dim socFile As String
Dim objFile As String
Dim temp(1024) As Byte
socFile = "C:\Documents and Settings\桌面\soc.txt"
objFile = "C:\Documents and Settings\桌面\obj.txt"
Open socFile For Binary As #1
Open objFile For Binary As #2
Do While Not EOF(1)
Get #1, , temp
Put #2, , temp
Loop
Close #2
Close #1
Debug.Print "socFile.len=" & FileLen(socFile)
Debug.Print "objfile.len=" & FileLen(objFile)
If FileLen(socFile) = FileLen(objFile) Then
Debug.Print "the same size!"
End If
End Sub
[/code]
以上代码产生的obj.txt的大小为何与原文件不同???怎样解决???(仍然希望每次读取1024的整数倍)
黑发的放牧者 2006-10-21 10:23
回复 #1 只爱陌生人 的帖子
Dim temp(1024) As Byte
是temp的问题,初始化的同时给temp赋值了
Nothing 2006-10-21 11:03
如果只是简单的文件读写,可以使用文件复制命令FileCopy
如果你还是想文件读写,可以使用动态数组
[code]
Dim socFile As String
Dim objFile As String
Dim temp() As Byte
socFile = "C:\Documents and Settings\桌面\soc.txt"
objFile = "C:\Documents and Settings\桌面\obj.txt"
Open socFile For Binary As #1
Open objFile For Binary As #2
ReDim temp(LOF(1))
Get #1, , temp
Put #2, , temp
Close #2
Close #1
Debug.Print "socFile.len=" & FileLen(socFile)
Debug.Print "objfile.len=" & FileLen(objFile)
If FileLen(socFile) = FileLen(objFile) Then
Debug.Print "the same size!"
End If
[/code]