因为好长时间没有搞过VB了,所以MM让我帮她今天写了段程序竟然不通(当然也没有写完了)。
要求:一个score的文本文件,每行存储一个学生的信息,包括学号,姓名,英语成绩,数学成绩,计算机成绩(各字段间用空格隔开);
现在要在每行末尾为学生加上总分和平均分。我的程序如下:请大家修改一下:module1:  
    Public Type StudentScore        num As Integer
    stuname As String
    english As Integer
    math As Integer
    pc As Integer
    total As Integer
    average As Integer    End TypeDim MyRecord As StudentScore
Private Sub Form_click()
            FileName = "c:\myext.txt"
      Open FileName For Random As #1 Len = oldlen
      'Text1.Text = Input$(LOF(1), #1)
      Do While Not EOF(1)   
          Get #1, , MyRecord 
      Text1.Text = MyRecord.average + MyRecord.english + MyRecord.math + MyRecord.num + MyRecord.pc + MyRecord.stuname + MyRecord.total
      Loop
      Close #1
End SubPrivate Sub Form_Load()
      oldlen = Len(MyRecord) - 4
      Text1.Text = oldlen
End Sub

解决方案 »

  1.   

    如果文件较小的话,可以采用以下办法,如果文件很大,建议采用文本数据库,另外,平均分没有处理,你可以自己处理一下(如小数点后保留一位有效数字)
    Private Sub Command1_Click()
        Dim s As String
        Dim englishscore As Double
        Dim mathscore As Double
        Dim pcscore As Double
        Dim totolcsore As Double
        Dim averagescore As Double
        Dim temparr() As Byte
        Dim oldlen As Long
        oldlen = FileLen(App.Path + "\chengji.txt")
        If oldlen = 0 Then Exit Sub
        ReDim temparr(oldlen - 1)
        '读取文件到字串s
        Open App.Path + "\chengji.txt" For Binary As #1
        Get #1, , temparr
        Close #1
        s = StrConv(temparr, vbUnicode)
        '处理数据
        Dim arrtemp As Variant
        arrtemp = Split(s, vbCrLf)
        Dim i As Long
        i = UBound(arrtemp)
        Dim scorearr() As String
        ReDim scorearr(i)
        Dim j As Long
        Dim linearr As Variant
        For j = 0 To i
            linearr = Split(arrtemp(j), " ")
            If UBound(linearr) = 4 Then
                englishscore = CDbl(linearr(2))
                mathscore = CDbl(linearr(3))
                pcscore = CDbl(linearr(4))
                totolcsore = englishscore + mathscore + mathscore
                averagescore = totolcsore / 3
                scorearr(j) = arrtemp(j) + " " + CStr(totolcsore) + " " + CStr(averagescore)
            Else
                scorearr(j) = arrtemp(j)
            End If
        Next
        Dim ss As String
        ss = ""
        If i = 0 Then
            ss = scorearr(0)
        Else
            For j = 0 To i - 1
            ss = ss + scorearr(j) + vbCrLf
            Next
            ss = ss + scorearr(i)
        End If
        MsgBox ss
        '写回文件
        Open App.Path + "\123.txt" For Binary As #2
        Put #2, , ss
        Close #2
    End Sub
      

  2.   

    Get #1, , MyRecord 
          Text1.Text = MyRecord.average + MyRecord.english + MyRecord.math + MyRecord.num + MyRecord.pc + MyRecord.stuname + MyRecord.total这里怎么才能保证取得的数据就是MyRecord呢?
      

  3.   

    FileName = "c:\myext.txt"
    FileName2 = "c:\myext2.txt"
    Open FileName For Random As #1 Len = oldlen
    Open FileName For Random As #2 Len = oldlen
    Do While Not EOF(1)   
        Get #1, , MyRecord 
        MyRecord.total = MyRecord.english + MyRecord.math + MyRecord.num + MyRecord.pc + MyRecord.stuname
        MyRecord.average = MyRecord.total / 5
        Put #2, , MyRecord
    Loop
    Close #1
    Close #2
      

  4.   

    Get #1, , MyRecord 
    MyRecord的数据类型定义不正确,正确也不行,要保证MyRecord的各个属性值是连续的。一定要用MyRecord 的话,需要使用定长字串,你的文件明显不符合要求