想知道如何用VB改写test.txt中某一行的内容,假设test.txt中有三行,内容如下
port=COM1
speed=19200
filename=abc.txt
想把speed=19200改写为speed=115200
VB有文件指针吗?请赐教
port=COM1
speed=19200
filename=abc.txt
想把speed=19200改写为speed=115200
VB有文件指针吗?请赐教
open mypath & "\test.txt" for output as #1
print #,"port=COM1"
print #,"speed=19200"
print #,"filename=abc.txt"
close #1
print #,"speed=115200"
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
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
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"