最近用vb在寫一個軟件,用到了對文件的寫入、替換、刪除操作,對網(wǎng)友的代碼進(jìn)行了修改,寫入模塊和函數(shù),能夠?qū)崿F(xiàn)上述功能。
有5個文本框,以前四個文本框的內(nèi)容為判別條件,即:如果保存的文件中某行前四項(以空格為區(qū)分)與文本框中的Text1、Text2、Text3、Text4相同,點擊“替換某行”按鈕可以將Text1-Text5中的內(nèi)容替換到原文件中。點擊“刪除某行”則刪除文件中與Text1-Text4相同的那一行。插入某行還沒有用到,沒有測試。
- Dim MonitorSetFile As String '文件名
- Private Sub Command1_Click()
- MonitorSetFile = App.Path + "\InstMonitorSet.dat"
- Dim ThisInst As String
- ThisInst = Text1.Text + " " + Text2.Text + " " + Text3.Text + " " + Text4.Text + Text5.Text
- Open MonitorSetFile For Append As #1 '以追加方式打開文件
- 'Print #1, '為防止原文件末尾沒有換行,而加入的換行
- Print #1, ThisInst '加入一個空行,為新加入內(nèi)容的加入時間,若不需要可刪除或注釋它
- 'Print #1, Text1.Text
- Close #1
- End Sub
- Private Sub Command2_Click()
- '替換
- MonitorSetFile = App.Path + "\InstMonitorSet.dat"
- Dim RowNumber As Long
- Call FindRow(RowNumber)
- Call ModifyInstSet(MonitorSetFile, MonitorSetFile, RowNumber, "替換")
- End Sub
- Function FindRow(ByRef FindTheRow As Long)
- Dim SmText As String
- Dim ThisYqSetInfo() As String
- Dim SearchHang As Long '查找待替換信息所在行
- Open MonitorSetFile For Input As #1
- Do While Not EOF(1)
- Line Input #1, SmText
- 'Form1.Print SmText
- SearchHang = SearchHang + 1
- ThisYqSetInfo = Split(SmText, " ") '將行文本內(nèi)容以空格為區(qū)分讀進(jìn)數(shù)組
- '判斷文本中是否與4個文本框的內(nèi)容一致,如果一致
- If ThisYqSetInfo(0) = Text1.Text And ThisYqSetInfo(1) = Text2.Text And ThisYqSetInfo(2) = Text3.Text And ThisYqSetInfo(3) = Text4.Text Then
- FindTheRow = SearchHang
- End If
- Loop
- Close #1
- 'Dim FileNumber As Integer '文件號
- 'Dim FiInfo() As String
- '計算源文件行數(shù)
- 'Dim FileHangSum As Integer '文件行數(shù)
- 'Dim FileHangText As String '文件某行文本
- 'Open MonitorSetFile For Input As #2
- ' Do While Not EOF(2)
- ' Line Input #2, FileHangText
- ' FileHangSum = FileHangSum + 1
- 'Loop
- 'Close #2
- 'Print "a文件中共有:" & FileHangSum & "行"
- End Function
- Private Sub Command3_Click()
- Dim DeleHang As String
- MonitorSetFile = App.Path + "\InstMonitorSet.dat"
- Dim RowNumber As Long
- '調(diào)用4個文本框所在行函數(shù)
- Call FindRow(RowNumber)
- Call ModifyInstSet(MonitorSetFile, MonitorSetFile, RowNumber, "刪除")
- End Sub
- Private Sub Command4_Click()
- '插入
- MonitorSetFile = App.Path + "\InstMonitorSet.dat"
- Dim RowNumber As Long
- Call FindRow(RowNumber)
- Call ModifyInstSet(MonitorSetFile, MonitorSetFile, RowNumber, "插入")
- End Sub
- Private Sub Form_Load()
- Command1.Caption = "寫入文件"
- Command2.Caption = "替換某行"
- Command1.Caption = "刪除某行"
- Command1.Caption = "插入某行"
- End Sub
將下面的代碼拷貝到標(biāo)準(zhǔn)模塊中:- '刪除、替換文件中一行,或者插入內(nèi)容到文本中某一行
- Public Function ModifyInstSet(strSourceFile As String, strTargetFile As String, intRow As Long, CommandCode As String)
- 'strSourceFile 原始文件完整名
- 'strTargetFile 生成新文件的完整名
- 'intRow 操作的行數(shù)
- Dim Filenum As Integer
- Dim FileContents As String
- Dim FileInfo() As String
- Dim ThI As Integer
- Dim ReplaceContent As String '要替換的文本
- '取出源文件行數(shù),按照回車換行來分隔成數(shù)組
- Filenum = FreeFile
- Open strSourceFile For Binary As #Filenum
- FileContents = Space(LOF(Filenum))
- Get #Filenum, , FileContents
- Close Filenum
- FileInfo = Split(FileContents, vbCrLf)
- '如果文件已存在則刪除原文件
- Filenum = FreeFile
- If Dir(strTargetFile, vbNormal) <> "" Then
- Kill strTargetFile
- End If
- '替換指定行
- If CommandCode = "替換" Then
- ReplaceContent = Form1.Text1.Text + " " + Form1.Text2.Text + " " + Form1.Text3.Text + " " + Form1.Text4.Text + " " + Form1.Text5.Text
- '替換一行代碼塊
- Open strTargetFile For Append As #Filenum
- '循環(huán)每一行
- For ThI = 0 To UBound(FileInfo) - 1
- If ThI = intRow - 1 Then
- Print #Filenum, ReplaceContent '替換的行
- Else
- Print #Filenum, FileInfo(ThI) '保留原來的行
- End If
- Next
- Close #Filenum
- MsgBox "替換完畢"
- '刪除指定行
- ElseIf CommandCode = "刪除" Then
- '刪除一行代碼塊
- Open strTargetFile For Append As #Filenum
- '循環(huán)每一行
- For ThI = 0 To UBound(FileInfo) - 1
- If ThI <> intRow - 1 Then
- Print #Filenum, FileInfo(ThI)
- End If
- Next
- Close #Filenum
- MsgBox "刪除完畢"
- '插入指定行
- ElseIf CommandCode = "插入" Then
- Open strTargetFile For Append As #Filenum
- InsertContent = Form1.Text1.Text + " " + Form1.Text2.Text + " " + Form1.Text3.Text + " " + Form1.Text4.Text + " " + Form1.Text5.Text
- '循環(huán)每一行
- For i = 0 To UBound(FileInfo) - 1
- If i = intRow - 1 Then
- Print #Filenum, InsertContent
- Print #Filenum, FileInfo(i) '保留原來的行,位置后移一位
- End If
- Next
- Close #Filenum
- MsgBox "插入完畢"
- End If
- End Function