我用二进制方式复制文件
    Dim Bit As Byte
    Open "d:\maplog.txt" For Binary As #1
    Open "d:\maplog1.txt" For Binary As #2
    While Not EOF(1)
        Get 1, , Bit
        Put 2, , Bit
    Wend
    Close 1
    Close 2
为什么最后复制出来的文件,最后总是多写一个字节,值为0
我用UE查看二进制,确实是最后多了一个字节,请问这是怎么回事?

解决方案 »

  1.   

    文件复制直接用FileCopy函数(VB内置)或者CopyFile(Windows API函数)去操作,比如:FileCopy "d:\maplog.txt", "d:\maplog1.txtCopyFile "d:\maplog.txt", "d:\maplog1.txt", 0用CopyFile函数需要加上API函数申明
    Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long如果文件内容已经在数组里面可以用
    ADO里面的Stream 或者 FSO里面的TextStream来操作
      

  2.   

    String赋值到Byte时,后面多了一个0。Private Sub Command1_Click()
        Dim Bit() As Byte
        Dim lByte As Long
        
        Open "\1.txt" For Binary As #1
        Open "\2.txt" For Binary As #2
        
        lByte = LOF(1)
        ReDim Bit(lByte - 1)
        
        Get #1, , Bit()
        Put #2, , Bit()
        
        Close #1
        Close #2
    End Sub
      

  3.   

    用vbs的读写文件就没这些事,不过速度慢
      

  4.   

    Dim Bit As Byte
      Open "d:\maplog.txt" For Binary As #1
      Open "d:\maplog1.txt" For Binary As #2
      Do
       Get #1, , Bit
       if EOF(1) then exit do
       Put #2, , Bit
      Loop
      Close #1
      Close #2不要使用
    while (条件)
    更不要使用
    while (组合条件)
    要使用
    while (1) {
     if (条件1) break;
     //...
     if (条件2) continue;
     //...
     if (条件3) return;
     //...
    }
    因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
    典型如:
    下面两段的语义都是当文件未结束时读字符
    whlie (!feof(f)) {
     a=fgetc(f);
     //...
     b=fgetc(f);//可能此时已经feof了!
     //...
    }
    而这样写就没有问题:
    whlie (1) {
     a=fgetc(f);
     if (feof(f)) break;
     //...
     b=fgetc(f);
     if (feof(f)) break;
     //...
    }
    类似的例子还可以举很多。
      

  5.   


      Dim Bit As Byte
      Dim n As Long
      Open "d:\maplog.txt" For Binary As #1
      Open "d:\maplog.txt" For Binary As #2
      n = LOF(1)
      While Loc(1) < n
      Get 1, , Bit
      Put 2, , Bit
      Wend
      Close 1
      Close 2
      

  6.   

    可否使用重命名的方式?比如是txt文件,把它改成dll文件,一般情况下是可以迷惑别人的。
      

  7.   

    仔细阅读以MSDN你会知道,为什么多了一个字节:
      

  8.   

    使用 EOF 是为了避免因试图在文件结尾处进行输入而产生的错误。直到到达文件的结尾,EOF 函数都返回 False。对于为访问 Random 或 Binary 而打开的文件,直到最后一次执行的 Get 语句无法读出完整的记录时,EOF 都返回 False。对于为访问 Binary 而打开的文件,在 EOF 函数返回 True 之前,试图使用 Input 函数读出整个文件的任何尝试都会导致错误发生。在用 Input 函数读出二进制文件时,要用 LOF 和 Loc 函数来替换 EOF 函数,或者将 Get 函数与 EOF 函数配合使用。对于为 Output 打开的文件,EOF 总是返回 True。
      

  9.   

    感觉不是EOF的问题,比如1.txt里输入abcde
    Private Sub Command1_Click()
        Dim Bit As Byte
        Open "c:\1.txt" For Binary As #1
        Open "c:\2.txt" For Binary As #2
        While Not EOF(1)
        Get 1, , Bit
        Put 2, , Bit
        MsgBox EOF(1) & "|" & Loc(1)
        Wend
        Close 1
        Close 2
    End Sub
    你会发现无法解释会出现 Fasle|5 这种情况,因为压根只有0-4,没有5,get语句怎么可能通过的呢,get出现的是什么呢???