分享

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

 hdzgx 2019-12-22

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


有5个文本框,以前四个文本框的内容为判别条件,即:如果保存的文件中某行前四项(以空格为区分)与文本框中的Text1、Text2、Text3、Text4相同,点击“替换某行”按钮可以将Text1-Text5中的内容替换到原文件中。点击“删除某行”则删除文件中与Text1-Text4相同的那一行。插入某行还没有用到,没有测试。

  1. Dim MonitorSetFile As String '文件名
  2. Private Sub Command1_Click()
  3. MonitorSetFile = App.Path + "\InstMonitorSet.dat"
  4. Dim ThisInst As String
  5. ThisInst = Text1.Text + " " + Text2.Text + " " + Text3.Text + " " + Text4.Text + Text5.Text
  6. Open MonitorSetFile For Append As #1 '以追加方式打开文件
  7. 'Print #1, '为防止原文件末尾没有换行,而加入的换行
  8. Print #1, ThisInst '加入一个空行,为新加入内容的加入时间,若不需要可删除或注释它
  9. 'Print #1, Text1.Text
  10. Close #1
  11. End Sub
  12. Private Sub Command2_Click()
  13. '替换
  14. MonitorSetFile = App.Path + "\InstMonitorSet.dat"
  15. Dim RowNumber As Long
  16. Call FindRow(RowNumber)
  17. Call ModifyInstSet(MonitorSetFile, MonitorSetFile, RowNumber, "替换")
  18. End Sub
  19. Function FindRow(ByRef FindTheRow As Long)
  20. Dim SmText As String
  21. Dim ThisYqSetInfo() As String
  22. Dim SearchHang As Long '查找待替换信息所在行
  23. Open MonitorSetFile For Input As #1
  24. Do While Not EOF(1)
  25. Line Input #1, SmText
  26. 'Form1.Print SmText
  27. SearchHang = SearchHang + 1
  28. ThisYqSetInfo = Split(SmText, " ") '将行文本内容以空格为区分读进数组
  29. '判断文本中是否与4个文本框的内容一致,如果一致
  30. If ThisYqSetInfo(0) = Text1.Text And ThisYqSetInfo(1) = Text2.Text And ThisYqSetInfo(2) = Text3.Text And ThisYqSetInfo(3) = Text4.Text Then
  31. FindTheRow = SearchHang
  32. End If
  33. Loop
  34. Close #1
  35. 'Dim FileNumber As Integer '文件号
  36. 'Dim FiInfo() As String
  37. '计算源文件行数
  38. 'Dim FileHangSum As Integer '文件行数
  39. 'Dim FileHangText As String '文件某行文本
  40. 'Open MonitorSetFile For Input As #2
  41. ' Do While Not EOF(2)
  42. ' Line Input #2, FileHangText
  43. ' FileHangSum = FileHangSum + 1
  44. 'Loop
  45. 'Close #2
  46. 'Print "a文件中共有:" & FileHangSum & "行"
  47. End Function
  48. Private Sub Command3_Click()
  49. Dim DeleHang As String
  50. MonitorSetFile = App.Path + "\InstMonitorSet.dat"
  51. Dim RowNumber As Long
  52. '调用4个文本框所在行函数
  53. Call FindRow(RowNumber)
  54. Call ModifyInstSet(MonitorSetFile, MonitorSetFile, RowNumber, "删除")
  55. End Sub
  56. Private Sub Command4_Click()
  57. '插入
  58. MonitorSetFile = App.Path + "\InstMonitorSet.dat"
  59. Dim RowNumber As Long
  60. Call FindRow(RowNumber)
  61. Call ModifyInstSet(MonitorSetFile, MonitorSetFile, RowNumber, "插入")
  62. End Sub
  63. Private Sub Form_Load()
  64. Command1.Caption = "写入文件"
  65. Command2.Caption = "替换某行"
  66. Command1.Caption = "删除某行"
  67. Command1.Caption = "插入某行"
  68. End Sub
将下面的代码拷贝到标准模块中:
  1. '删除、替换文件中一行,或者插入内容到文本中某一行
  2. Public Function ModifyInstSet(strSourceFile As String, strTargetFile As String, intRow As Long, CommandCode As String)
  3. 'strSourceFile 原始文件完整名
  4. 'strTargetFile 生成新文件的完整名
  5. 'intRow 操作的行数
  6. Dim Filenum As Integer
  7. Dim FileContents As String
  8. Dim FileInfo() As String
  9. Dim ThI As Integer
  10. Dim ReplaceContent As String '要替换的文本
  11. '取出源文件行数,按照回车换行来分隔成数组
  12. Filenum = FreeFile
  13. Open strSourceFile For Binary As #Filenum
  14. FileContents = Space(LOF(Filenum))
  15. Get #Filenum, , FileContents
  16. Close Filenum
  17. FileInfo = Split(FileContents, vbCrLf)
  18. '如果文件已存在则删除原文件
  19. Filenum = FreeFile
  20. If Dir(strTargetFile, vbNormal) <> "" Then
  21. Kill strTargetFile
  22. End If
  23. '替换指定行
  24. If CommandCode = "替换" Then
  25. ReplaceContent = Form1.Text1.Text + " " + Form1.Text2.Text + " " + Form1.Text3.Text + " " + Form1.Text4.Text + " " + Form1.Text5.Text
  26. '替换一行代码块
  27. Open strTargetFile For Append As #Filenum
  28. '循环每一行
  29. For ThI = 0 To UBound(FileInfo) - 1
  30. If ThI = intRow - 1 Then
  31. Print #Filenum, ReplaceContent '替换的行
  32. Else
  33. Print #Filenum, FileInfo(ThI) '保留原来的行
  34. End If
  35. Next
  36. Close #Filenum
  37. MsgBox "替换完毕"
  38. '删除指定行
  39. ElseIf CommandCode = "删除" Then
  40. '删除一行代码块
  41. Open strTargetFile For Append As #Filenum
  42. '循环每一行
  43. For ThI = 0 To UBound(FileInfo) - 1
  44. If ThI <> intRow - 1 Then
  45. Print #Filenum, FileInfo(ThI)
  46. End If
  47. Next
  48. Close #Filenum
  49. MsgBox "删除完毕"
  50. '插入指定行
  51. ElseIf CommandCode = "插入" Then
  52. Open strTargetFile For Append As #Filenum
  53. InsertContent = Form1.Text1.Text + " " + Form1.Text2.Text + " " + Form1.Text3.Text + " " + Form1.Text4.Text + " " + Form1.Text5.Text
  54. '循环每一行
  55. For i = 0 To UBound(FileInfo) - 1
  56. If i = intRow - 1 Then
  57. Print #Filenum, InsertContent
  58. Print #Filenum, FileInfo(i) '保留原来的行,位置后移一位
  59. End If
  60. Next
  61. Close #Filenum
  62. MsgBox "插入完毕"
  63. End If
  64. End Function



    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多