访问随机文件.frm

来自「蒋加伏主编VB程序设计第四版PPT及全部课本源码 北京邮电大学出版社 」· FRM 代码 · 共 105 行

FRM
105
字号
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "例[12-5]访问随机文件"
   ClientHeight    =   2490
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   3135
   LinkTopic       =   "Form1"
   ScaleHeight     =   2490
   ScaleWidth      =   3135
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text3 
      Height          =   495
      Left            =   1440
      TabIndex        =   6
      Top             =   1320
      Width           =   1455
   End
   Begin VB.TextBox Text2 
      Height          =   495
      Left            =   1440
      TabIndex        =   4
      Top             =   720
      Width           =   1455
   End
   Begin VB.TextBox Text1 
      Height          =   495
      Left            =   1440
      TabIndex        =   2
      Top             =   120
      Width           =   1455
   End
   Begin VB.CommandButton Command1 
      Caption         =   "读一条记录"
      Height          =   495
      Left            =   780
      TabIndex        =   0
      Top             =   1920
      Width           =   1575
   End
   Begin VB.Label Label3 
      Caption         =   "语文成绩:"
      Height          =   375
      Left            =   240
      TabIndex        =   5
      Top             =   1440
      Width           =   975
   End
   Begin VB.Label Label2 
      Alignment       =   2  'Center
      Caption         =   "姓    名:"
      Height          =   375
      Left            =   240
      TabIndex        =   3
      Top             =   840
      Width           =   975
   End
   Begin VB.Label Label1 
      Alignment       =   2  'Center
      Caption         =   "文件长度:"
      Height          =   375
      Left            =   240
      TabIndex        =   1
      Top             =   240
      Width           =   975
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Command1_Click()
  Dim person1 As ID
  Dim i As Integer
  Cls
  Open "d:\Score.dat" For Random As #1 Len = Len(person1)
  Text1.Text = LOF(1)
  Randomize
  i = Int(4 * Rnd + 1)             'i的取值范围为1~4
  Get #1, i, person1               '读第i条记录
  Text2.Text = person1.Name
  Text3.Text = person1.Score
  Close #1
End Sub
Private Sub Form_Click()
  '该事件过程建立d:\Score.dat随机文件
  Dim person1 As ID            '声明自定义类型变量
  Open "d:\Score.dat" For Random As #1 Len = Len(person1)  '建立随机文件
  person1.Name = "张强"
  person1.Score = 87
  Put #1, 1, person1           '写入1号文件的第1条记录
  person1.Name = "李小明"
  person1.Score = 97
  Put #1, 2, person1               '写入1号文件的第2条记录
  person1.Name = "王平"
  person1.Score = 83
  Put #1, 3, person1                '写入1号文件的第3条记录
  person1.Name = "赵志钢"
  person1.Score = 72
  Put #1, 4, person1                '写入1号文件的第4条记录
  Close #1
End Sub

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?