vb使用open方法读写文件
vb使用open方法读写文件
(一)打开和关闭文件
1、顺序文件
打开顺序文件,我们可以使用Open语句。它的格式如下:
Open pathname For [Input |Output |Append] As [#]filenumber [Len = buffersize]
说明:
(1)参数pathname 表示要打开的文件名,文件名可以包含有驱动器和目录
(2)Input Output
和Append用于设置顺序文件的打开方式。其中,Input表示从打开的文件中读取数据。以这种方式打开文件时,文件必须存在,否则会产生错误。
Output表示向打开的文件中写入数据。以这种方式打开文件时,文件中原有的数据将被覆盖,新的数据将从文件开始写入。如果文件不存在,则创建一个新文
件。Append表示向打开的文件中添加数据。以这种方式打开时,文件中原有的数据将被保留,新的数据将从文件为开始添加。如果文件不存在,则创建一个新
文件。
(3)As[#]filenumber 子句用于为打开的文件指定文件号.对文件进行读写操作时,要用文件号表示该文件.文件号是介于1~511之间的整数,既可以是数字,又可以是变量.也可以省略不用.
(4)当在文件与程序之间拷贝数据时,Len=buffersize子句指定缓冲区的字符数.
例如:
Open App.Path + "\test.dat" For Output As 1
Open App.Path + "\test.dat" For Output As 1
这两句代码在当前应用程序所在目录下创建了一个名为test.dat的文本文件,分配文件号为1.
Open App.Path + "\test.dat" For Input As [#]filenumber
这条语句是从文本文件中读取数据.
Open App.Path + "\test.dat" For Append As [#]filenumber
这条语句则是像文本文件中添加数据
2、随机文件
操作随机文件之前,首先必须定义用于保存数据项的记录类型.该记录是用户自定义数据类型,他们是随机文件中存储数据的基本结构.例如:
Type Student
No As Integer
Name As String * 20
age As Integer
End Type
Dim Stud As Student ‘定义一个可以存放学生材料的变量
随机文件中,所有的数据都将保存到若干个结构为Student类型的记录中, 而从随机文件中读出的数据则可以存放到变量Stud中.
之后我们就可以打开并读写文件了.下面是打开随机文件的语法格式:
Open filename For Random as [#]filenumber Len = Reclengt
相关文档:
Private Sub Command1_Click()
Dim dso As New XMLDSOControl
Dim doc As IXMLDOMDocument
Set doc = dso.XMLDocument
Dim rel As IXMLDOMNodeList
Set x = CreateObject("Microsoft.XMLHTTP")
x.Open "GET", "http://127.0.0.1:8080/emrb/Mrbmodel?action=dis&u ......
帮朋友改的一小段关键词分析代码; 含两个单词复合计数
Private Function CollectWords() As Dictionary(Of String, Integer)
'Create a new dictionary
Dim table As New Dictionary(Of String, Integer)
'Prompt for the user
Console.WriteLine(
"Enter a line : ")
'Get the user's input
Dim input As St ......
VERSION 5.00
Begin VB.Form frmMain
BorderStyle = 1 'Fixed Single
Caption = "Reg Demo"
ClientHeight = 6570
ClientLeft = 45
ClientTop = 435
ClientWidth = 7695
LinkTopic = "Form1"
MaxButton = 0 'False
Min ......
内容提要摘要: The software of visual basic developed by Microsoft corporation is becoming one of the main develop tools at today。 As it's remarkable peculiarity, the Grid control has very great practical and active use。 This topic discusses how to use the grid control of VB to develop prati ......
在VB中要想调用Excel,需要打开VB编程环境“工程”菜单中的“引用”项目,并选取项目中的“Microsoft Excel 11.0 object library”项。由于你的Excel版本不同,所以这个选项的版本号也是不同的。
因为EXCEL是以层次结构组织对象的,其对象模型中含有许多不同的对象元素。
&n ......