想知道如何用VB改写test.txt中某一行的内容,假设test.txt中有三行,内容如下
port=COM1
speed=19200
filename=abc.txt
想把speed=19200改写为speed=115200
VB有文件指针吗?请赐教

解决方案 »

  1.   

    你这个文件是以顺序文件的格式写的。改写实际上就是重新创建文件:
    open mypath & "\test.txt" for output as #1
    print #,"port=COM1"
    print #,"speed=19200"
    print #,"filename=abc.txt"
    close #1
      

  2.   

    第二句应:
    print #,"speed=115200"
      

  3.   

    难道就没有更好的办法了吗?C好象没那么复杂,VB更不会了,我坚信有更好的办法,还是谢谢楼上的
      

  4.   

    读取内容,替换后写回Private Sub Command1_Click()
        Dim strTmp As String
        Open "c:\test.txt" For Binary As #1
            strTmp = Space(LOF(1))
            Get #1, 1, strTmp
        Close #1
        Debug.Print strTmp
        strTmp = Replace(strTmp, "speed=19200", "speed=115200")
        Debug.Print strTmp
        Open "c:\test.txt" For Binary As #1
            Put #1, 1, strTmp
        Close #1
    End Sub
      

  5.   

    如果有SectionName,你的文件可以使用下面2个API按INI格式读写:Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
      

  6.   

    按楼主的文件内容用楼上的方法直接读写INI文件比较方便
      

  7.   

    给你个完整的ini读写
    Declare Function WritePrivateProfileString _
    Lib "kernel32" Alias "WritePrivateProfileStringA" _
    (ByVal lpApplicationName As String, ByVal _
    lpKeyName As Any, ByVal lsString As Any, _
    ByVal lplFilename As String) As LongDeclare Function GetPrivateProfileString Lib _
    "kernel32" Alias "GetPrivateProfileStringA" _
    (ByVal lpApplicationName As String, ByVal _
    lpKeyName As String, ByVal lpDefault As _
    String, ByVal lpReturnedString As String, _
    ByVal nSize As Long, ByVal lpFileName As _
    String) As LongPublic Sub LoadINI()
    On Error Resume Next
    Dim lngResult As Long
    Dim strfileName
    Dim strResult As String * 300
    strfileName = App.Path & "\SettingsEx.ini" 'Declare your ini file !
    lngResult = GetPrivateProfileString(KeySection, _
    KeyKey, strfileName, strResult, Len(strResult), _
    strfileName)
    If lngResult = 0 ThenKeyValue = ""
    Else
    KeyValue = Trim(strResult)
    End IfEnd SubPublic Sub SaveINI()
    On Error Resume Next
    Dim lngResult As Long
    Dim strfileName
    strfileName = App.Path & "\SettingsEx.ini" 'Declare your ini file !
    lngResult = WritePrivateProfileString(KeySection, _
    KeyKey, KeyValue, strfileName)
    If lngResult = 0 Then
    'An error has occurred
    Call MsgBox("读写配置文件出错,配置文件保存失败!", vbExclamation, ZX)
    End IfEnd Sub
      

  8.   

    port=COM1
    speed=19200
    filename=abc.txt
    想把speed=19200改写为speed=115200
    VB中无法指定写Txt文件内容,要写的话,就只能全部写了!
    port=COM1
    speed=115200
    filename=abc.txtDim Fsote As New FileSystemObject, Tste As TextStream
    Dim Txths As Integer, Txtnr As String
    读文件时,可以指定读哪一行!
        Set Tste = Fsote.OpenTextFile(App.Path + "\AppDefine.ini", 1)
        Txths = 3
        For jsq = 1 To Txths 
            '逐行读出来
            Txtnr = Trim(Tste.ReadLine)
            '下面可以加上内容的处理与过滤
        Next jsq写文件也只能逐行写了!
        Set Tste = Fsote.CreateTextFile(App.Path + "\AppDefine.ini", True)
        Tste.WriteLine "port=COM1"
        Tste.WriteLine "speed=115200"
        Tste.WriteLine "filename=abc.txt"
      

  9.   

    还是用了2楼的方法,看来除了API就是二进制,没有简单方法.估计是VB对内存操作不好造成的,谢过各位