VB从文本中读取变量并赋值

现在有1.txt文件,存放在C盘根目录。里面内容为:Tongxue17552000Tongxue27004-100Tongxue365782400Tongxue46372... 现在有1.txt文件,存放在C盘根目录。

里面内容为:

Tongxue1 755 2 0 0 0
Tongxue2 700 4 -1 0 0
Tongxue3 657 8 24 0 0
Tongxue4 637 29 16 0 0

要求从这个文件中 读取“Tongxue1”作为一个“student”变量,755,2,0,0,0分别作为student的“grade”"rank""drank""goal""mark"属性(student已经type完了)
然后读取Tongxue2,Tongxue3...

也就是说,每行第一个字符串当做变量名,剩下的当属性。每行是一个变量。

最后用Listbox把这些变量连同属性显示出来

——————————————————————
谢谢大家,急用
展开
 我来答
WWEERR
2011-04-04 · TA获得超过841个赞
知道小有建树答主
回答量:536
采纳率:0%
帮助的人:529万
展开全部
程序已经做完,如果发上来不审核的话就直接复制了,要审核的话腾讯号1014580870

Private Type Student
name As String
grade As Long
rank As Long
drank As Long
goal As Long
mark As Long
End Type
Dim StudentNumber As Long
Dim stu() As Student
Private Sub Form_Load()

Dim ThisStu As String
Open "c:\1.txt" For Input As #1 '记总数
Do Until EOF(1)
Line Input #1, ThisStu
StudentNumber = StudentNumber + 1 '记总数
Loop
Close #1
ReDim stu(0 To StudentNumber) '重定义
StudentNumber = 0
ThisStu = ""
Open "c:\1.txt" For Input As #1
Do Until EOF(1)
StudentNumber = StudentNumber + 1
Line Input #1, ThisStu
stu(StudentNumber).name = Mid(ThisStu, 1, InStr(1, ThisStu, " ") - 1) '更新
ThisStu = Mid(ThisStu, InStr(1, ThisStu, " ") + 1) '更新字符串

stu(StudentNumber).grade = CLng(Mid(ThisStu, 1, InStr(1, ThisStu, " "))) '更新
ThisStu = Mid(ThisStu, InStr(1, ThisStu, " ") + 1) '更新字符串

stu(StudentNumber).rank = Mid(ThisStu, 1, InStr(1, ThisStu, " ")) '更新
ThisStu = Mid(ThisStu, InStr(1, ThisStu, " ") + 1) '更新字符串

stu(StudentNumber).drank = Mid(ThisStu, 1, InStr(1, ThisStu, " ")) '更新
ThisStu = Mid(ThisStu, InStr(1, ThisStu, " ") + 1) '更新字符串

stu(StudentNumber).goal = Mid(ThisStu, 1, InStr(1, ThisStu, " ")) '更新
ThisStu = Mid(ThisStu, InStr(1, ThisStu, " ") + 1) '更新字符串

stu(StudentNumber).mark = CLng(ThisStu) '更新
ThisStu = Mid(ThisStu, InStr(1, ThisStu, " ") + 1) '更新字符串

Loop
Close #1

For i = 1 To StudentNumber
List1.AddItem stu(i).name
Next
End Sub
Private Sub List1_Click()
List2.Clear
List2.AddItem "grade: " & stu(List1.ListIndex + 1).grade
List2.AddItem "rank : " & stu(List1.ListIndex + 1).rank
List2.AddItem "drank: " & stu(List1.ListIndex + 1).drank
List2.AddItem "goal : " & stu(List1.ListIndex + 1).goal
List2.AddItem "mark : " & stu(List1.ListIndex + 1).mark
End Sub

'text在c:\1.txt ,两个List 分别为list2 ,list1 list1显示学生名,list2显示学生的信息
'stu()数组是储存学生的信息的数组,可以通过stu(编号) 访问
chinaboyzyq
2011-04-04 · TA获得超过1.3万个赞
知道大有可为答主
回答量:1.3万
采纳率:89%
帮助的人:3251万
展开全部
Private Type Student
name As String
grade As Integer
rank As Integer
drank As Integer
goal As Integer
mark As Integer
End Type

Private Sub Form_Load()
Dim std() As Student, Num As Integer
Dim istr As String, istr1() As String
Dim x As Integer

Open "c:\1.txt" For Binary As #1
istr = StrConv(InputB(LOF(1), 1), vbUnicode)
istr = Replace(istr, vbCrLf, " ")
istr1 = Split(istr)
Num = UBound(istr1) / 6 - 1
ReDim std(Num)
For i = 0 To Num
std(i).name = istr1(i + x)
std(i).grade = istr1(i + x + 1)
std(i).rank = istr1(i + x + 2)
std(i).drank = istr1(i + x + 3)
std(i).goal = istr1(i + x + 4)
std(i).mark = istr1(i + x + 5)
x = x + 5
Next

For i = 0 To i - 1
List1.AddItem _
Format(std(i).name, "@@@@@@@@@@!") & " " & _
Format(std(i).grade, "@@@@@") & " " & _
Format(std(i).rank, "@@@@@") & " " & _
Format(std(i).drank, "@@@@@") & " " & _
Format(std(i).goal, "@@@@@") & " " & _
Format(std(i).mark, "@@@@@")
Next

End Sub
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
ljl88900
2011-04-04 · TA获得超过2661个赞
知道大有可为答主
回答量:2197
采纳率:100%
帮助的人:2628万
展开全部
VB没这个功能,VFP有。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式