VB编写一个随机文件程序
2.读出RANDOM.DAT文件中的内容,然后按成绩排序,最后按顺序写入另一个随机文件(RANDOM1.DAT)
3.再次读出文件的内容,按文件中的顺序将学生的信息显示在屏幕上,检查正确性 展开
Private Type stu
num As String * 10
name As String * 20
cj As Long
End Type
Private Sub Command1_Click()
Dim f As String, f1 As String, stud As stu, studd(5) As stu
' Print Len(stud)
f = App.Path & "\RANDOM.DAT"
Open f For Random As #1 Len = 35
For j = 1 To 5
stud.num = Format(Now(), "yyyymmdd") & Format(j, "00")
stud.name = "学生" & j
stud.cj = 90 + j
Put #1, j, stud
Next
Close #1
Print "原始数据:"
Open f For Random As #1 Len = 35
For j = 1 To 5
Get #1, j, studd(j)
Print studd(j).num; studd(j).name; studd(j).cj
Next
Close #1
For j = 1 To 5
For k = j + 1 To 5
If studd(j).cj <= studd(k).cj Then
stud = studd(j)
studd(j) = studd(k)
studd(k) = stud
End If
Next
Next
f = App.Path & "\RANDOM1.DAT"
Open f For Random As #1 Len = 35
For j = 1 To 5
Put #1, j, studd(j)
Next
Close #1
Print "排序后数据:"
Open f For Random As #1 Len = 35
For j = 1 To 5
Get #1, j, studd(j)
Print studd(j).num; studd(j).name; studd(j).cj
Next
Close #1
End Sub
推荐于2018-03-19
If Trim(Text1.Text) = "" Or Trim(Text2.Text) = "" Or Trim(Text3.Text) = "" Then
MsgBox "学生信息不能为空!"
Exit Sub
End If
Open App.Path & "\Random.dat" For Append As #1
Write #1, Text1.Text, Text2.Text, CSng(Text3.Text)
Close #1
i = 0
paixu
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text1.SetFocus
End Sub
Private Sub paixu()
Dim a() As String
Dim n() As String
Dim t As Integer
Dim k As Integer
Dim u As String
i = 0
List1.Clear
Open App.Path & "\Random.dat" For Input As #1
Do While Not EOF(1)
Line Input #1, textline
i = i + 1
Loop
Close #1
If i = 0 Then
Exit Sub
End If
ReDim a(i - 1)
i = 0
Open App.Path & "\Random.dat" For Input As #1
Do While Not EOF(1)
Line Input #1, textline
a(i) = textline
i = i + 1
Loop
Close #1
For i = 0 To UBound(a) - 1
For j = i + 1 To UBound(a)
n = Split(a(i), ",")
t = n(2)
n = Split(a(j), ",")
k = n(2)
If t > k Then
u = a(i)
a(i) = a(j)
a(j) = u
End If
Next
Next
Open App.Path & "\Random1.dat" For Output As #1
For i = 0 To UBound(a)
List1.AddItem a(i)
Print #1, a(i)
Next
Close #1
End Sub
Private Sub Form_Load()
paixu
End Sub