首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

在TXT文件中删除第一行,再将字符串插入到第一行,怎么办,

2012-02-06 
在TXT文件中删除第一行,再将字符串插入到第一行,怎么处理,求助.例如a.txt中内容是:1abcdefg想要助 张三

在TXT文件中删除第一行,再将字符串插入到第一行,怎么处理,求助.
例如a.txt中内容是:

1
abc
defg

想要助 "张三 "代替第一行的1,得到

"张三 "
abc
defg

怎么写呢

[解决办法]
dim strLine as string
open "c:\test.txt " for input as #1
line input #1,strLine
open "c:\test1.txt " for output as #2
print #2, "你要替换的第一行内容 "
do while not eof(1)
line input #1,strLine
print #2,strLine
loop
close #1
close #2
kill "c:\test.txt "
Name "c:\test1.txt " as "c:\test.txt "
[解决办法]
Private Sub Command1_Click()
Open "c:\1.txt " For Input As #1
Do Until EOF(1) = True
Line Input #1, a
b = b & a & vbCrLf
Loop
Close #1
Print b
Dim aa As Variant
aa = Split(b, vbCrLf)
aa(0) = "张三 "
Open "c:\2.txt " For Output As #1

For i = 0 To UBound(aa)

bb = bb & aa(i) & vbCrLf
Next i
Print #1, bb
Close #1
End Sub
[解决办法]
引用Microsoft Scriptting Runtime
Private Sub Form_Load()
Dim fso As New FileSystemObject
Dim f As File
Dim ts As TextStream
Set f = fso.GetFile( "d:\1.txt ")
Set ts = f.OpenAsTextStream(ForReading)
Dim str1 As String
str1 = ts.ReadAll
str1 = Replace(str1, Mid(str1, 1, InStr(str1, vbCrLf) - 1), "张三 ")
Set ts = f.OpenAsTextStream(ForWriting)
ts.Write str1
End Sub

热点排行