Private Type RECORD YEAR AS LONG ‘年 YW AS LONG ‘语文 SX AS LONG ‘数学 YY AS LONG ‘英语 TY AS LONG ‘体育 YY AS LONG ‘音乐 End TypePrivate Type RECORD XH(1 To 6) As Byte‘学号 XM(1 To 9) As Byte ‘姓名 End Type 2个type不应同名。 YY AS LONG ‘英语 TY AS LONG ‘体育 YY AS LONG ‘音乐2个字段也不应同名。建议用ACCESS OR DBF 数据库
按行读取文件,再用split分解每一行。 ' 文本文件类(TextFile.cls) Option ExplicitDim FileNum As Integer 'local variable(s) to hold property value(s) Private mvarFileName As String 'local copy Public Property Let FileName(ByVal vData As String) 'used when assigning a value to the property, on the left side of an assignment. 'Syntax: X.FileName = 5 mvarFileName = vData End Property Public Property Get FileName() As String 'used when retrieving value of a property, on the right side of an assignment. 'Syntax: Debug.Print X.FileName FileName = mvarFileName End Property ' 打开文件用于写 Public Function OpenWrit(fn As String) As Boolean On Error GoTo OWErr FileNum = FreeFile Open fn For Output Access Write As #FileNum OpenWrit = True
Exit Function OWErr: OpenWrit = False End Function' 打开文件用于写 Public Function OpenAPPE(fn As String) As Boolean On Error GoTo OWErr FileNum = FreeFile Open fn For Append Access Write As #FileNum OpenAPPE = True Exit Function OWErr: OpenAPPE = False End Function' 打开文件用于读 Public Function OpenRead(fn As String) As Boolean On Error GoTo ORErr
FileNum = FreeFile Open fn For Input Access Read As #FileNum OpenRead = True
Exit Function ORErr: OpenRead = False End Function' 关闭文件 Public Sub CloseFile() If FileNum > 0 Then Close #FileNum FileNum = 0 End If End Sub' 从文件中读取一行 Public Function GetLine(S As String) As Boolean On Error GoTo NotGet If EOF(FileNum) Then GoTo NotGet Line Input #FileNum, S GetLine = True
Exit Function NotGet: GetLine = False End Function' 往文件中写入一行 Public Function PutLine(ByVal S As String) As Boolean On Error GoTo NotPut
Print #FileNum, S PutLine = True Exit Function NotPut: PutLine = False End Function' 类在结束时,自动关闭文件 Private Sub Class_Terminate() CloseFile End Sub Public Sub log(msg1 As String) Dim lineStr As String lineStr = Now lineStr = lineStr + " " + msg1 OpenAPPE FileName PutLine lineStr CloseFile End SubPublic Function getLastLine() As String Dim strValue As String Dim Ret As Boolean OpenRead FileName Do Ret = GetLine(strValue) If Ret = False Then Exit Do Loop CloseFile getLastLine = strValue End Function
YEAR AS LONG ‘年
YW AS LONG ‘语文
SX AS LONG ‘数学
YY AS LONG ‘英语
TY AS LONG ‘体育
YY AS LONG ‘音乐
End TypePrivate Type RECORD
XH(1 To 6) As Byte‘学号
XM(1 To 9) As Byte ‘姓名
End Type
2个type不应同名。
YY AS LONG ‘英语
TY AS LONG ‘体育
YY AS LONG ‘音乐2个字段也不应同名。建议用ACCESS OR DBF 数据库
' 文本文件类(TextFile.cls)
Option ExplicitDim FileNum As Integer
'local variable(s) to hold property value(s)
Private mvarFileName As String 'local copy
Public Property Let FileName(ByVal vData As String)
'used when assigning a value to the property, on the left side of an assignment.
'Syntax: X.FileName = 5
mvarFileName = vData
End Property
Public Property Get FileName() As String
'used when retrieving value of a property, on the right side of an assignment.
'Syntax: Debug.Print X.FileName
FileName = mvarFileName
End Property
' 打开文件用于写
Public Function OpenWrit(fn As String) As Boolean
On Error GoTo OWErr FileNum = FreeFile
Open fn For Output Access Write As #FileNum
OpenWrit = True
Exit Function
OWErr:
OpenWrit = False
End Function' 打开文件用于写
Public Function OpenAPPE(fn As String) As Boolean
On Error GoTo OWErr FileNum = FreeFile
Open fn For Append Access Write As #FileNum
OpenAPPE = True
Exit Function
OWErr:
OpenAPPE = False
End Function' 打开文件用于读
Public Function OpenRead(fn As String) As Boolean
On Error GoTo ORErr
FileNum = FreeFile
Open fn For Input Access Read As #FileNum
OpenRead = True
Exit Function
ORErr:
OpenRead = False
End Function' 关闭文件
Public Sub CloseFile()
If FileNum > 0 Then
Close #FileNum
FileNum = 0
End If
End Sub' 从文件中读取一行
Public Function GetLine(S As String) As Boolean
On Error GoTo NotGet If EOF(FileNum) Then GoTo NotGet
Line Input #FileNum, S
GetLine = True
Exit Function
NotGet:
GetLine = False
End Function' 往文件中写入一行
Public Function PutLine(ByVal S As String) As Boolean
On Error GoTo NotPut
Print #FileNum, S
PutLine = True Exit Function
NotPut:
PutLine = False
End Function' 类在结束时,自动关闭文件
Private Sub Class_Terminate()
CloseFile
End Sub
Public Sub log(msg1 As String)
Dim lineStr As String
lineStr = Now
lineStr = lineStr + " " + msg1
OpenAPPE FileName
PutLine lineStr
CloseFile
End SubPublic Function getLastLine() As String
Dim strValue As String
Dim Ret As Boolean
OpenRead FileName
Do
Ret = GetLine(strValue)
If Ret = False Then Exit Do
Loop
CloseFile
getLastLine = strValue
End Function