| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1712 人关注过本帖
标题:文本文件查找替换问题
只看楼主 加入收藏
jiangyawen
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2007-8-31
收藏
得分:0 
对,就是这样,好崇拜你哦
谢谢!
太感激了!
要认你做老大!

2007-09-05 14:19
jiangyawen
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2007-8-31
收藏
得分:0 
为什么后面又设target="slore"呢
开始的 target = "SIP Port :"

2007-09-05 14:22
slore
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1108
专家分:0
注 册:2005-7-1
收藏
得分:0 

我是懒的在浪费一个变量来保存 已经找到了并替换了。。
target后面的部分不需要在用它了,所以修改它,到后面判断即可知道替换过了,就不重新追加了。


当然你可以引入个布尔变量(新变量)表示。

快上课了……
2007-09-05 14:25
jiangyawen
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2007-8-31
收藏
得分:0 

Private Sub CmdApply_Click()
Dim Text1 As String
Dim Text2 As String
Dim Text3 As String
Dim Text4 As String
Dim Text5 As String
Dim Text6 As String
Dim Text8 As String
Dim Text9 As String
Text1 = "<SIP CONFIG MODULE>"
Text2 = "SIP Port :" & TextLocalSIPPort.Text
Text3 = "Stun Address :" & TextSTUNServerAddr.Text
Text4 = "Stun Port :" & TextSTUNServerPort.Text
Text5 = "Stun Effect Time :" & TextSTUNEffectTime.Text
Text6 = "--SIP Line List-- :"
Text8 = "SIP1 Use Stun :" & CheckUseStun.Value
Text9 = "SIP2 Use Stun :" & CheckUseStun1.Value
'text2的替换模块
Dim FileNum As Long
Dim StrLine As String
Dim StrFileContent As String
Dim target As String
'获取用户输入的要查找的字符串
target = "SIP Port :"
FileNum = FreeFile
Open ".\Config.txt" For Input As #FileNum
Do Until EOF(FileNum)
Line Input #FileNum, StrLine
If InStr(StrLine, target) > 0 Then StrLine = Text2: target = "Slore"
StrFileContent = StrFileContent & StrLine & vbCrLf
Loop
Close #FileNum
Open ".\Config.txt" For Output As #FileNum
Print #FileNum, StrFileContent
Close #FileNum
If target <> "Slore" Then '没有找到了匹配字符串
Open ".\config.txt" For Append As #FileNum
Print #FileNum, Text1
Print #FileNum, Text2
End If
Close #FileNum
'text3的替换模块

Dim StrFileContent1 As String
Dim target1 As String
'获取用户输入的要查找的字符串
target1 = "Stun Address :"
Open ".\Config.txt" For Input As #FileNum
Do Until EOF(FileNum)
Line Input #FileNum, StrLine
If InStr(StrLine, target) > 0 Then StrLine = Text3: target1 = "Slore1"
StrFileContent = StrFileContent & StrLine & vbCrLf
Loop
Close #FileNum
Open ".\Config.txt" For Output As #FileNum
Print #FileNum, StrFileContent
Close #FileNum
If target1 <> "Slore1" Then '没有找到了匹配字符串
Open ".\config.txt" For Append As #FileNum
Print #FileNum, Text3
End If
Close #FileNum
Unload Stun
End Sub


我想拓展可就是不行,反而把前面的都搞错了!
帮我看看行不?真不想麻烦你,可是我实在是没办法,你谅解下哈


2007-09-05 14:45
jiangyawen
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2007-8-31
收藏
得分:0 
回复:(jiangyawen)文本文件查找替换问题

Private Sub CmdApply_Click()
Dim Text1 As String
Dim Text2 As String
Dim Text3 As String
Dim Text4 As String
Dim Text5 As String
Dim Text6 As String
Dim Text8 As String
Dim Text9 As String
Text1 = "<SIP CONFIG MODULE>"
Text2 = "SIP Port :" & TextLocalSIPPort.Text
Text3 = "Stun Address :" & TextSTUNServerAddr.Text
Text4 = "Stun Port :" & TextSTUNServerPort.Text
Text5 = "Stun Effect Time :" & TextSTUNEffectTime.Text
Text6 = "--SIP Line List-- :"
Text8 = "SIP1 Use Stun :" & CheckUseStun.Value
Text9 = "SIP2 Use Stun :" & CheckUseStun1.Value
'text2的替换模块
Dim FileNum As Long
Dim StrLine As String
Dim StrFileContent As String
Dim target As String
'获取用户输入的要查找的字符串
target = "SIP Port :"
FileNum = FreeFile
Open ".\Config.txt" For Input As #FileNum
Do Until EOF(FileNum)
Line Input #FileNum, StrLine
If InStr(StrLine, target) > 0 Then StrLine = Text2: target = "Slore"
StrFileContent = StrFileContent & StrLine & vbCrLf
Loop
Close #FileNum
Open ".\Config.txt" For Output As #FileNum
Print #FileNum, StrFileContent
Close #FileNum
If target <> "Slore" Then '没有找到了匹配字符串
Open ".\config.txt" For Append As #FileNum
Print #FileNum, Text1
Print #FileNum, Text2
End If
Close #FileNum
'text3的替换模块
Dim target1 As String
'获取用户输入的要查找的字符串
target1 = "Stun Address :"
Open ".\Config.txt" For Input As #FileNum
Do Until EOF(FileNum)
Line Input #FileNum, StrLine
If InStr(StrLine, target1) > 0 Then StrLine = Text3: target1 = "Slore1"
StrFileContent = StrFileContent & StrLine & vbCrLf
Loop
Close #FileNum
Open ".\Config.txt" For Output As #FileNum
Print #FileNum, StrFileContent
Close #FileNum
If target1 <> "Slore1" Then '没有找到了匹配字符串
Open ".\config.txt" For Append As #FileNum
Print #FileNum, Text3
End If
Close #FileNum
Unload Stun
End Sub

修改了下最明显的错误,但结果还是一样!


2007-09-05 14:50
jiangyawen
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2007-8-31
收藏
得分:0 

<<VOIP CONFIG FILE>>Version:2.0002


<SIP CONFIG MODULE>
SIP Port :43

Stun Address :4325
可以了,但就是怎么才能变成
<<VOIP CONFIG FILE>>Version:2.0002

<SIP CONFIG MODULE>
SIP Port :43
Stun Address :4325
这个样子呢,就是在代码怎么会有这么多的换行呢?
奇怪


2007-09-05 14:54
slore
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:1108
专家分:0
注 册:2005-7-1
收藏
得分:0 

Private Sub CmdApply_Click()
Dim Text1 As String
Dim
Text2 As String
Dim
Text3 As String
Dim
Text4 As String
Dim
Text5 As String
Dim
Text6 As String
Dim
Text8 As String
Dim
Text9 As String

Text1 = "<SIP CONFIG MODULE>"
Text2 = "SIP Port :" & TextLocalSIPPort.Text
Text3 = "Stun Address :" & TextSTUNServerAddr.Text
Text4 = "Stun Port :" & TextSTUNServerPort.Text
Text5 = "Stun Effect Time :" & TextSTUNEffectTime.Text
Text6 = "--SIP Line List-- :"
Text8 = "SIP1 Use Stun :" & CheckUseStun.Value
Text9 = "SIP2 Use Stun :" & CheckUseStun1.Value

'Call ADDData("SIP Port :", Text2)
'Call ADDData("Stun Address :", Text3)
'Unload Stun
'End Sub
'Private Sub ADDData(target As String, NewStr As String)
Dim FileNum As Long
Dim
StrLine As String
Dim
StrFileContent As String
'-----------------------------
'-----------------------------
'-----------------------------
Dim target As String
Dim
target1 As String

target = "SIP Port :" '多个目标在这里添加
target1 = "Stun Address :" '因为看你的项目不多,所以直接代码添加本来写成过程的……还有数组什么的……
'-----------------------------
'-----------------------------
'-----------------------------
FileNum = FreeFile
Open "Config.txt" For Input As #FileNum
Do Until EOF(FileNum)
Line Input #FileNum, StrLine
If StrLine <> "" Then
'在这里过滤就可以了……不要全部重新多遍……本来写成Sub了,要和上面对应
'-----------------------------
'-----------------------------
'-----------------------------
If InStr(StrLine, target) > 0 Then StrLine = Text2: target = "Slore"
If InStr(StrLine, target1) > 0 Then StrLine = Text3: target1 = "Slore1"
'-----------------------------
'-----------------------------
'-----------------------------
StrFileContent = StrFileContent & StrLine & vbCrLf '这里的缘故所以每在最后会多一个空行
End If
Loop
Close #
FileNum
If StrFileContent <> "" Then '如果不是空文件
'在写如前,我们把最后的换行回车符号去掉
StrFileContent = Left$(StrFileContent, Len(StrFileContent) - 2)
Open "Config.txt" For Output As #FileNum
Print #FileNum, StrFileContent '好像print写入最后是有个空回车
Close #FileNum
End If

Open "config.txt" For Append As #FileNum
If target <> "Slore" Then '没有找到了匹配字符串
Print #FileNum, Text1
Print #FileNum, Text2
End If
If
target1 <> "Slore1" Then '没有找到了匹配字符串
Print #FileNum, Text3
End If
Close #
FileNum
'End Sub
Unload Stun
End Sub
发现print写是会有一个空行的。你看下这个吧。只有最后的一个空换行

对了open还是写完整路径,不然类似快捷方式等打开会找不到Config.txt


快上课了……
2007-09-05 15:50
快速回复:文本文件查找替换问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037612 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved