主题:vb读取txt中的数据文件
云顶晓风
[专家分:0] 发布于 2011-05-14 22:13:00
请教下大家,我想用vb编程读取txt1的数据文件(格式如下),想读取里面12.5和12.5,然后在程序中计算:
area=12.5*12.5
perimeter=2*(12.5+12.5)
再将这两个结果写入txt2文件中,请教下这个该如何编程,衷心感谢!
txt1:
Fence Input File
The length is: 12.5
The width is: 12.5
txt2:
Fence Output File
The area is: 156.250000
The perimeter is: 50.000000
回复列表 (共1个回复)
沙发
一江秋水 [专家分:9680] 发布于 2011-05-15 09:34:00
Dim s1 As Single, s2 As Single
Private Sub 读_Click()
Dim st As String, i As Integer
Open "D:\1.txt" For Input As #1 '假设要读取的文件为 D:\1.txt
Do Until EOF(1)
Line Input #1, st
i = InStr(st, "The length is: "): If i Then s1 = Val(Mid(st, InStr(st, ":") + 1))
i = InStr(st, "The width is: "): If i Then s2 = Val(Mid(st, InStr(st, ":") + 1))
Loop
Close #1
End Sub
Private Sub 写_Click()
Dim st As String, area As String, perimeter As String
area = Format(s1 * s2, "###0.000000")
perimeter = Format(2 * (s1 + s2), "###0.000000")
st = "Fence Output File" & vbCrLf
st = st & "The area is: " & area & vbCrLf
st = st & "The perimeter is: " & perimeter
Open "D:\2.txt" For Output As #1 '假设要写盘的文件为 D:\2.txt
Print #1, st
Close #1
End Sub
我来回复