插入替换删除文本文件中的某一行

'在网上找了关于这方面的资料,发现跟本用不起来

'后来我拷下来,一个一个调试,终于达到预想的效果

 '立此存照 2008-7-15       Jason

 

Private Sub Form_Load()
Dim strtemp As String
Open App.Path & "/18XX.txt" For Input As #1
 Do Until EOF(1)
        Line Input #1, strtemp
       Text1.Text = Text1.Text + strtemp + vbCrLf
    Loop
    
    Close #1
End Sub

Private Sub Command1_Click()   '读取不含注解的第几行
Dim Strtmp As String   '定义Strtmp为字符型
Dim flag1 As Boolean   '定义标识符
Dim i As Long          '定义i为长型
Dim a As Long          '定义a为长型
Dim xxxx

Open App.Path & "/18XX.txt" For Input As #1   '打开文件路径
 i = 0                         '标识关
Do While Not EOF(1)                           '开始循环
    Line Input #1, Strtmp                     '行输入
 
    If Mid(Strtmp, 1, 1) = "#" Then '如果某一行的前四个字符为"■■■■"
    Else
          i = i + 1
          If i = Val(Text3.Text) Then    'i 值从文本框输入数字
          Text1.Text = Strtmp            '显示结果
          Exit Do
          End If
          TimeDelay 200                   '累加延时读入
           Text4.Text = i + 1 '累加
       If i >= Val(Text4.Text) Then
       
       End If
       End If
Loop
Close #1
End Sub

'■■■■■■■■■■■■■■■■■■■■■■■■删除一行■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Private Sub Command3_Click()
     Call DELETE(App.Path & "/18XX.txt", "~", Val(Text5.Text))  '调用删除子程序
End Sub
'strSourceFile  原始文件完整名
'strTargetFile  生成新文件的完整名
'intRow         操作的行数
Sub DELETE(strSourceFile As String, strTargetFile As String, intRow As Long)
    Dim filenum         As Integer
    Dim fileContents    As String
    Dim fileInfo()      As String
    Dim i               As Integer
    Dim j               As Integer
    Dim aa
    filenum = FreeFile
    Open strSourceFile For Binary As #filenum
        fileContents = Space(LOF(filenum))
        Get #filenum, , fileContents
    Close filenum
    fileInfo = Split(fileContents, vbCrLf)    '取出源文件行数,按照回车换行来分隔成数组
    
    filenum = FreeFile
    &apos;If Dir(strTargetFile, vbNormal) <> "" Then   &apos;这句的意是,如果想要建立的临时文件同名的文件存在的话,就把已存在的文件删除
    
     &apos;   Kill strTargetFile
    &apos;End If
    Dim Filestr() As String
    
    &apos;删除一行代码块
    strTargetFile = "1409.txt"                    &apos;把临时文件命名为1409.txt
    Open strTargetFile For Append As #filenum     &apos;以追加的方式打开文件
        &apos;循环每一行
        For i = 0 To UBound(fileInfo) - 1         &apos;行循环至结束
            If i <> intRow - 1 Then
                Print #filenum, fileInfo(i)
            End If
        Next
    Close #filenum
    aa = strSourceFile
    Kill strSourceFile                      &apos;把原来的文件删除
    Name strTargetFile As aa                &apos;把临时文件名改为原来的文件名
        
     Text1.Text = ""                      &apos;清空text1
   Dim strtemp As String
Open App.Path & "/18XX.txt" For Input As #1    &apos;把所有内容读入text1,以观看结果
 Do Until EOF(1)
    Line Input #1, strtemp
    Text1.Text = Text1.Text + strtemp + vbCrLf
    Loop
    Close #1
    
    &apos;MsgBox "删除完成!"
End Sub
&apos;■■■■■■■■■■■■■■■■■■■■■■■■删除一行■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

&apos;■■■■■■■■■■■■■■■■■■■■■■■■替换一行■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Private Sub Command4_Click()
 Call COVER(App.Path & "/18XX.txt", Text2.Text, Val(Text5.Text))
End Sub
&apos;strSourceFile  原始文件完整名
&apos;strTargetFile  生成新文件的完整名
&apos;intRow         操作的行数
Sub COVER(strSourceFile As String, strTargetFile As String, intRow As Long)
    Dim filenum         As Integer
    Dim fileContents    As String
    Dim fileInfo()      As String
    Dim i               As Integer
    Dim j               As Integer
    Dim aa
    filenum = FreeFile
    Open strSourceFile For Binary As #filenum
        fileContents = Space(LOF(filenum))
        Get #filenum, , fileContents
    Close filenum
    fileInfo = Split(fileContents, vbCrLf)
    &apos;取出源文件行数,按照回车换行来分隔成数组
    
    filenum = FreeFile
   &apos; If Dir(strTargetFile, vbNormal) <> "" Then
   &apos;     Kill strTargetFile
   &apos; End If
    Dim Filestr() As String
    
    &apos;替换一行代码块
    strTargetFile = "1409.txt"
     Open strTargetFile For Append As #filenum
        &apos;循环每一行
        For i = 0 To UBound(fileInfo) - 1
           
            If i = intRow - 1 Then
                Print #filenum, Text2.Text
               Else
                Print #filenum, fileInfo(i)  &apos;保留原来的行,位置后移一位
            End If
        Next i
    Close #filenum
   
    aa = strSourceFile
    Kill strSourceFile
   Name strTargetFile As aa
    
    Text1.Text = ""
   Dim strtemp As String
Open App.Path & "/18XX.txt" For Input As #1
 Do Until EOF(1)
    Line Input #1, strtemp
    Text1.Text = Text1.Text + strtemp + vbCrLf
    Loop
    Close #1
    
    &apos;MsgBox "替换完成!"
End Sub
&apos;■■■■■■■■■■■■■■■■■■■■■■■■替换一行■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

&apos;■■■■■■■■■■■■■■■■■■■■■■■■插入一行■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
Private Sub Command5_Click()
Call INSERTTXT(App.Path & "/18XX.txt", Text6.Text, Val(Text5.Text))
End Sub
&apos;strSourceFile  原始文件完整名
&apos;strTargetFile  生成新文件的完整名
&apos;intRow         操作的行数
Sub INSERTTXT(strSourceFile As String, strTargetFile As String, intRow As Long)
    Dim filenum         As Integer
    Dim fileContents    As String
    Dim fileInfo()      As String
    Dim i               As Integer
    Dim j               As Integer
    Dim aa
    filenum = FreeFile
    Open strSourceFile For Binary As #filenum
        fileContents = Space(LOF(filenum))
        Get #filenum, , fileContents
    Close filenum
    fileInfo = Split(fileContents, vbCrLf)
    &apos;取出源文件行数,按照回车换行来分隔成数组
    
    filenum = FreeFile
    &apos;If Dir(strTargetFile, vbNormal) <> "" Then
    &apos;   Kill strTargetFile
   &apos; End If
    Dim Filestr() As String
    
    &apos;插入一行代码块
    strTargetFile = "1409.txt"
     Open strTargetFile For Append As #filenum
        &apos;循环每一行
        
         For i = 0 To UBound(fileInfo) - 1
            If i = intRow - 1 Then
                Print #filenum, Text6.Text
                Else
                   
                 End If
                 Print #filenum, fileInfo(i) &apos;保留原来的行,位置后移一位
        Next i
    Close #filenum
   
    aa = strSourceFile
    Kill strSourceFile
   Name strTargetFile As aa
    
    Text1.Text = ""
   Dim strtemp As String
Open App.Path & "/18XX.txt" For Input As #1
 Do Until EOF(1)
    Line Input #1, strtemp
    Text1.Text = Text1.Text + strtemp + vbCrLf
    Loop
    Close #1
    
    &apos;MsgBox "插入完成!"
End Sub
&apos;■■■■■■■■■■■■■■■■■■■■■■■■插入一行■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

Private Sub Command6_Click()
Unload Me
End
End Sub
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值