vb6 对文件的写入、替换、删除和插入某行操作

最近用vb在写一个软件,用到了对文件的写入、替换、删除操作,对网友的代码进行了修改,写入模块和函数,能够实现上述功能。


有5个文本框,以前四个文本框的内容为判别条件,即:如果保存的文件中某行前四项(以空格为区分)与文本框中的Text1、Text2、Text3、Text4相同,点击“替换某行”按钮可以将Text1-Text5中的内容替换到原文件中。点击“删除某行”则删除文件中与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        '加入一个空行,为新加入内容的加入时间,若不需要可删除或注释它
  '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, " ")   '将行文本内容以空格为区分读进数组
        '判断文本中是否与4个文本框的内容一致,如果一致
           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

'计算源文件行数
'Dim FileHangSum As Integer    '文件行数
'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
'调用4个文本框所在行函数
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
将下面的代码拷贝到标准模块中:

'删除、替换文件中一行,或者插入内容到文本中某一行
Public Function ModifyInstSet(strSourceFile As String, strTargetFile As String, intRow As Long, CommandCode As String)
'strSourceFile  原始文件完整名
'strTargetFile  生成新文件的完整名
'intRow         操作的行数
   Dim Filenum         As Integer
   Dim FileContents    As String
   Dim FileInfo()      As String
   Dim ThI               As Integer
   Dim ReplaceContent As String  '要替换的文本
   
   '取出源文件行数,按照回车换行来分隔成数组
   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
       '循环每一行
       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
       '循环每一行
       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
   
       '循环每一行
       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



  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值