如题
在blue.bas中将str写入文本文件res.txt:
str="abcdefg"
Dim lenth
lenth = Len(str)
Open res.txt For Binary As #1
Put #1, lenth, str
Close #1
在db.csl中读文本文件res.txt以便得到str:
    Dim connstr2 As String
    Open res.txt For Binary As #1
    Get #1, lof(1), connstr2
    Close #1
但无法执行
因为这段代码要用于和数据库连接的(str里写的是与数据库连接的语句) 但总没办法连接上

解决方案 »

  1.   

    '读写二进制文件
    Private Sub Command1_Click()
        Dim n As Long
        Dim arrBytes() As Byte
        
        '读出数据
        Open "d:\draw.ico" For Binary As 1
            n = LOF(1)
            ReDim arrBytes(1 To n) As Byte
            Get 1, , arrBytes
        Close 1
            '写入数据
        Open "d:\aaa.ico" For Binary As 2
            Put 2, , arrBytes
        Close 2
        
    End Sub
      

  2.   

    Private Sub Command2_Click()
       Dim str As String
        str = "abcdefg"
        Dim lenth
        lenth = Len(str)
        Open "C:\res.txt" For Binary As #1
       Put #1, lenth, str
        Close #1
    '在db.csl中读文本文件res.txt以便得到str:
        ReDim connstr2(lenth)
        Dim connstr3 As String
        Dim i As Integer
        Open "C:\res.txt" For Binary As #1
        Get #1, , connstr2(0)
        Close #1
    End Sub
      

  3.   

    不明白,为什么ReDim connstr2(lenth)
    而在读的时候要Get #1, , connstr2(0)
    下标从0开始?
    可不可以Get #1, , connstr2(lenth)?
      

  4.   

    我修改了一下,各位大侠请帮我看一下
    write.bas:
    Public Function blue()
    Dim str As String
    'str是与数据库连接用的字符串
    str = "aa;bb;cc;dd;ee"
    Dim j() As Integer
    Dim a2() As Byte
    For i = 0 To (Len(str) - 1)
    j(i) = Left(str, 1)
    lenth1 = Len(str)
    str = Right(str, lenth1 - 1)
    a2(i) = CByte(Asc(j(i)))
    Next i
    Open "d:\res.txt" For Binary As #1
    Put #1, , a2
    Close #1
    End Function
    READ.cls:
    Public Function db()
        Dim conn As New ADODB.Connection
        Dim a() As Byte
        Open "d:\res.txt" For Binary As #1
        Get #1, LOF(1), a
        Close #1
        '与数据库连接的字符串内有82个字符
        Dim s As String * 82
        For i = 0 To 81
        s = s & Chr(a(UBound(a) - i))
        Next
        s = StrReverse(s)
        conn.Open s
        Set db = conn
    End Function
      

  5.   

    Dim j() As Integer
    Dim a2() As Byte
    For i = 0 To (Len(str) - 1)
    j(i) = Left(str, 1)
    lenth1 = Len(str)
    str = Right(str, lenth1 - 1)
    a2(i) = CByte(Asc(j(i)))
    Next i这段有问题,首先,效率太低(直接a2=str就可以了),其次有错误,用了动态数组,就应该给其分配内存空间(用redim语句)
      

  6.   

    Dim Cnn As ADODB.Connection
    Dim Rs As ADODB.Recordset
    Dim FileName As String
    Dim Datafile As IntegerDim Flong As Long
    Dim Chuncks As Integer
    Const chunksize = 16384
    Dim Fragment As Integer
    Dim chunk() As BytePrivate Sub Command1_Click()
    WriteBlob
    End SubPrivate Sub Command2_Click()
    GetBlob
    End SubPrivate Sub Form_Load()
    Set Cnn = New ADODB.Connection
    Cnn.ConnectionString = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=pubs"
    Cnn.OpenSet Rs = New ADODB.Recordset
    Rs.Open "niu", Cnn, adOpenStatic, adLockOptimistic, adCmdTable
    End Sub
    Private Sub WriteBlob()
    FileName = "D:\读书\图标\icon-1700\coin\动物\02.ico"
    Datafile = 1Open FileName For Binary Access Read As DatafileRs.MoveFirst
    Flong = LOF(Datafile)
    If Flong = 0 Then Close Datafile: Exit SubChuncks = Flong \ chunksize
    Fragment = Flong Mod chunksizeRs(1).AppendChunk Null
    ReDim chunk(Fragment)
    Get Datafile, , chunk()
    Rs(1).AppendChunk chunk()ReDim chunk(chunksize)
    For i = 1 To Chuncks
    Get Datafile, , chunk()
    Rs(1).AppendChunk chunk()
    Next i
    Close Datafile
    End Sub
    Private Sub GetBlob()
    Datafile = 2
    FileName = "D:\读书\图标\icon-1700\coin\动物\02.ico"'Open FileName For Binary Access Write As Datafile
    'Rs.MoveFirst
    '
    'Flong = Rs(1).ActualSize
    'Chuncks = Flong \ chunksize
    'Fragment = Flong Mod chunksize
    'ReDim chunk(Fragment)
    'chunk() = Rs(1).GetChunk(Fragment)
    '
    'Put Datafile, , chunk()
    'ReDim chunk(chunksize)
    'For i = 1 To Chuncks
    'chunk() = Rs(1).GetChunk(chunksize)
    'Put Datafile, , chunk()
    'Next i
    'Close Datafile
    Picture1.Picture = LoadPicture(FileName)
    End Sub
      

  7.   

    我对这样写和读一个二进制文件的用途不是很明了.如果是想将用于数据库连接的内容放在一个文件中,为什么不声明一个结构体?用随机函数来做?或者干脆用INI来做?
      

  8.   

    读写二进制文件不能用String
    只能用Byte数组因为VB会自动对String进行UniCode转换
    会导致数据错误同时由于UniCode的String中
    无论是中文字符还是英文字符
    都占一字符、两字节
    (而不是传统的汉字占两字节,英文占一字节)
    所以容易引起地址错误
      

  9.   

    回复人: LILITH2004(卡蜜拉) ( ) 信誉:100  2004-01-16 14:35:00  得分:0 
     
     
      不明白,为什么ReDim connstr2(lenth)
    而在读的时候要Get #1, , connstr2(0)
    下标从0开始?
    可不可以Get #1, , connstr2(lenth)?
      
     
    ========================================这是由于你还没有为connstr2指定数据类型
    所以connstr2是Variant数组,所以只需要该数组的一个元素就行了
    文件读写时,Variant会自动绑定为String现在很多外行人到计算机基础图书中出书捞钱
    导致Bug满天飞
    连“Windows 98是真正的32位操作系统”都写入了教育大纲
      

  10.   

    用Open res.txt For Binary As #1就可以了
      

  11.   

    我binary也用了,unicode也转换了,就是读写不了文件啊:(
    各位高手帮帮小妹吧,再交不来我就要死翘翘拉:(