请问:如何用VB实现读取TXT文件指定列数据?
如图:并将得到这6个数值得出一个平均值,与一给定值(92.3)比较看是否在一个上下限之间(±0.4)?请在关键地方注释一下,本人初学VB。txt文件路径在哪里指定?...
如图:
并将得到这6个数值得出一个平均值,与一给定值(92.3)比较看是否在一个上下限之间(±0.4)?
请在关键地方注释一下,本人初学VB。
txt文件路径在哪里指定? 展开
并将得到这6个数值得出一个平均值,与一给定值(92.3)比较看是否在一个上下限之间(±0.4)?
请在关键地方注释一下,本人初学VB。
txt文件路径在哪里指定? 展开
展开全部
Private Sub Command1_Click()
Dim FileName As String, Fn As Integer, i As Long, S As String
Dim Begin As Boolean, Pos As Long, a As Single, Js As Single
Dim Bz As Single, Wc As Single
FileName = "2Ksens.txt" '文本文件
Bz = 92.3 '标准值
Wc = 0.4 '误差
If Dir(FileName) = "" Then Exit Sub
Js = 0
a = 0
Fn = FreeFile
Open FileName For Input As #Fn
Do While Not EOF(Fn)
Line Input #Fn, S
If Begin = True Then
S = RTrim(S)
S = Mid(S, Pos)
If S <> "" Then
a = a + Val(S)
Js = Js + 1
End If
End If
If InStr(S, "NaN") Then
Pos = InStr(S, "NaN")
Begin = True
End If
Loop
Close #Fn
a = a / Js
a = a - Bz
If a >= -Wc And a <= Wc Then
MsgBox "平均值与一给定值(92.3)比较,在一个上下限之间(±0.4)!", vbInformation, "提示"
Else
MsgBox "平均值与一给定值(92.3)比较,不在一个上下限之间(±0.4)!", vbCritical, "警告"
End If
End Sub
Dim FileName As String, Fn As Integer, i As Long, S As String
Dim Begin As Boolean, Pos As Long, a As Single, Js As Single
Dim Bz As Single, Wc As Single
FileName = "2Ksens.txt" '文本文件
Bz = 92.3 '标准值
Wc = 0.4 '误差
If Dir(FileName) = "" Then Exit Sub
Js = 0
a = 0
Fn = FreeFile
Open FileName For Input As #Fn
Do While Not EOF(Fn)
Line Input #Fn, S
If Begin = True Then
S = RTrim(S)
S = Mid(S, Pos)
If S <> "" Then
a = a + Val(S)
Js = Js + 1
End If
End If
If InStr(S, "NaN") Then
Pos = InStr(S, "NaN")
Begin = True
End If
Loop
Close #Fn
a = a / Js
a = a - Bz
If a >= -Wc And a <= Wc Then
MsgBox "平均值与一给定值(92.3)比较,在一个上下限之间(±0.4)!", vbInformation, "提示"
Else
MsgBox "平均值与一给定值(92.3)比较,不在一个上下限之间(±0.4)!", vbCritical, "警告"
End If
End Sub
更多追问追答
追问
运行后 没出现Msgbox?是将FileName = "2Ksens.txt"换成路径名对吧?
追答
没出现Msgbox,可能是文件名称不对,请修改FileName为正确的文件名即可。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这就是正则表示啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |