就业数据资源平台
当前位置:首页 > Visual Basic
2011年计算机等级考试二级VB考点及习题(6)

 §7、文件管理控件

  文件处理 顺序文件和随机文件的打开(open)语句,EOF函数,读写语句。


  do while not eof(1) do until eof(1)


  print #1, write #1,


  input #1,a,b,c line input #1,s input(#1,100)


  get #1,a put #1,”af”


  打开方式:


  顺序文件Open "文件路径+文件名(包括文件后缀)" For Output As #1-511


  Open "文件路径+文件名(包括文件后缀)" For Append As #1-511


  Open "文件路径+文件名(包括文件后缀)"For Input As #1-511


  随机文件:Open "文件路径+文件名(包括文件后缀)"For Random As #1-511


  二进制文件:Open "文件路径+文件名(包括文件后缀)"For Binary As #1-511


  Open语句打开文件的注意:


  1.如果以Output、Append、Random、Binary模式打开一个不存在的文件时,VB自动创建一个相应文件;


  2.在Input Random Binary模式下,可以用不同的文件号打开同一文件,但Output Append方式不可以;


  3.所有当前使用的文件号必须是唯一的;


  4.如果以Output模式打开一个已存在的文件,即使没有进行写操作原来数据也将被覆盖。


  Close语句注意:(Reset)


  1.直接使用 Close语句(缺省文件号),则所有用Open语句打开的活动文件都被关闭;


  2.当程序结束时,所有打开的文件自动被关闭。


  Lock和Unlock语句注意:


  1.对于二进制文件,Lock和Unlock的是字节范围;


  2.对于随机文件,Lock和Unlock的是记录范围;


  3.对于顺序文件,Lock和Unlock的是整个文件,即使指明范围也不起作用;


  4.缺省情况下Lock和Unlock的范围是整个文件;


  5.Lock和Unlock总是成对使用。


  特别注意:在关闭文件或结束程序之前,必须用Unlock语句对先前锁定的文件解锁,否则会产生难以预料的错误。


  试题


  1.要对顺序文件进行写操作,下列打开文件语句中正确的是( )。


  Open "file1.txt" for output as #1


  Open "file1.txt" for input as #1


  Open "file1.txt" for Random as #1


  Open "file1.txt" for binary as #1


  2.在VB中文件访问的类型有( )。


  A.顺序、随机、二进制 B.顺序、随机、字符


  A. 顺序、十六进制、随机 D.顺序、记录、字符


  3.将文件Data1中的无序数据和Data2中的有序数据插入到Data3中,要求:Data3中的数据必须也是排好序。请填写空白处以实现该题功能。


  Private Sub Form_Click()


  Dim a() As Integer, b() As Integer


  Dim k As Integer, i As Integer


  Open "e:\data1.txt" For Input As #2


  Open "e:\data2.txt" For Input As #3


  Do While Not EOF(2)


  k = k + 1


  ReDim Preserve a(k)


  Input #2, a(k)


  Loop


  _______k=0______________________


  Do While Not EOF(3)


  k = k + 1


  ReDim Preserve b(k)


  Input #3, b(k)


  Loop


  Call insert(a, b)


  Open "e:\data3.txt" For Output As #4


  _k=ubound(b)____________________________


  For i = 1 To k


  Print #4, b(i)


  Next i


  Close


  End Sub


  Private Sub insert(a() As Integer, b() As Integer)


  Dim m As Integer, i As Integer


  Dim n As Integer, j As Integer


  m = UBound(a): n = UBound(b)


  _redim preserve b(m+n)_______________________


  i = 0


  Do While i < m


  i = i + 1


  j = n


  Do While j >= 1 And a(i) < b(j)


  If a(i) < b(j) Then


  ___b(j+1)=b(j)_______________________


  j = j - 1


  Else


  Exit Do


  End If


  Loop


  __b(j+1)=a(i)___________________________


  n = n + 1


  Loop


  End Sub

就业数据资源平台