当前位置: 源码素材网 » VB教程 » 详情页

VB随机读取文件、随机写入文件

  •   时间:2019-06-08
  • 概述:读取文件 写入文件

VB随机读取文件、随机写入文件,主要演示了两种功能,写文件和读文件,后附有完整的源代码。

VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "随机文件的读写"
   ClientHeight    =   3630
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   5625
   LinkTopic       =   "Form1"
   ScaleHeight     =   3630
   ScaleWidth      =   5625
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command2 
      Caption         =   "读文件"
      Height          =   495
      Left            =   3000
      TabIndex        =   1
      Top             =   2520
      Width           =   1215
   End
   Begin VB.CommandButton Command1 
      Caption         =   "写文件"
      Height          =   495
      Left            =   960
      TabIndex        =   0
      Top             =   2520
      Width           =   1215
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Base 1
Private Type club  '声明自定义类型
clubID As String * 3
    ClubName As String * 8
    DtmBirth As Date
End Type
Private Sub Command1_Click()
    Dim stu(2) As club  '声明自定义类型的数组
    Open "123.txt" For Random As #1 Len = Len(stu(1)) '以随机方式打开文件
    stu(1).ClubName = "曼联队" '给数组元素赋值
    stu(1).DtmBirth = #2/1/1890#
    stu(2).ClubName = "巴塞罗那队"
    stu(2).DtmBirth = #12/1/2001#
    Put #1, 1, stu(1) '把数组元素写入文件中
    Put #1, 2, stu(2)
    Close 1
End Sub
Private Sub Command2_Click()
    Dim stu As club  '声明自定义类型的变量
    Open "123.txt" For Random As #1 Len = Len(stu) '打开随机文件
    Get #1, , stu  '读入一个记录并显示内容
    Print stu.ClubName, stu.DtmBirth
    Get #1, , stu  '读入另一个记录并显示内容
    Print stu.ClubName, stu.DtmBirth
Close 1
End Sub

    相关声明:

      若“VB随机读取文件、随机写入文件”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。