重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
VB
德保ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!
二进制,八进制,十进制,十六进制转换
-
程序开发
-
ASP.NET教程
Public
Function
D_To_B(ByVal
Dec
As
Long)
As
String
Do
D_To_B
=
Dec
Mod
2
D_To_B
Dec
=
Dec
\
2
Loop
While
Dec
End
Function
Public
Function
B_To_D(ByVal
Bin
As
String)
As
Integer
Dim
i
As
Long
For
i
=
1
To
Len(Bin)
B_To_D
=
B_To_D
*
2
+
Val(Mid(Bin,
i,
1))
Next
i
End
Function
Public
Function
H_To_B(ByVal
Hex
As
String)
As
String
Dim
i
As
Long
Dim
B
As
String
Hex
=
UCase(Hex)
For
i
=
1
To
Len(Hex)
Select
Case
Mid(Hex,
i,
1)
Case
"0":
B
=
B
"0000"
Case
"1":
B
=
B
"0001"
Case
"2":
B
=
B
"0010"
Case
"3":
B
=
B
"0011"
Case
"4":
B
=
B
"0100"
Case
"5":
B
=
B
"0101"
Case
"6":
B
=
B
"0110"
Case
"7":
B
=
B
"0111"
Case
"8":
B
=
B
"1000"
Case
"9":
B
=
B
"1001"
Case
"A":
B
=
B
"1010"
Case
"B":
B
=
B
"1011"
Case
"C":
B
=
B
"1100"
Case
"D":
B
=
B
"1101"
Case
"E":
B
=
B
"1110"
Case
"F":
B
=
B
"1111"
End
Select
Next
i
While
Left(B,
1)
=
"0"
B
=
Right(B,
Len(B)
-
1)
Wend
H_To_B
=
B
End
Function
Public
Function
B_To_H(ByVal
Bin
As
String)
As
String
Dim
i
As
Long
Dim
H
As
String
If
Len(Bin)
Mod
4
Then
Bin
=
String(4
-
Len(Bin)
Mod
4,
"0")
Bin
End
If
For
i
=
1
To
Len(Bin)
Step
4
Select
Case
Mid(Bin,
i,
4)
Case
"0000":
H
=
H
"0"
Case
"0001":
H
=
H
"1"
Case
"0010":
H
=
H
"2"
Case
"0011":
H
=
H
"3"
Case
"0100":
H
=
H
"4"
Case
"0101":
H
=
H
"5"
Case
"0110":
H
=
H
"6"
Case
"0111":
H
=
H
"7"
Case
"1000":
H
=
H
"8"
Case
"1001":
H
=
H
"9"
Case
"1010":
H
=
H
"A"
Case
"1011":
H
=
H
"B"
Case
"1100":
H
=
H
"C"
Case
"1101":
H
=
H
"D"
Case
"1110":
H
=
H
"E"
Case
"1111":
H
=
H
"F"
End
Select
Next
i
B_To_H
=
H
End
Function
本示例阐释二进制文件的基本输入和输出(使用 BinaryReader、BinaryWriter 和 FileStream 类。 在如何创建日志文件标题下面有一个类似的主题。读写二进制信息使您可以创建和使用通过其他输入和输出方法无法访问的文件。本示例还展示写入非字符串数据,并展示二进制 I/O 的功能。
尽管计算机上的文件可以不同的类型和文件存储,但是,二进制格式是文件的较常用格式之一。此处对创建二进制文件的简短介绍使用基类 BinaryReader 和 BinaryWriter 从文件获取信息,并将信息放入文件。这些类中的每个类均封装一个信息流,因此,在进一步操作之前,需要创建一个可用于来回写信息的流。因为要创建文件,所以可使用 FileStream 来公开特定文件,在此情况下,如果该文件已存在,则可以修改该文件,或者如果该文件尚不存在,则可以创建该文件。在有 FileStream 之后,可以使用它来构造 BinaryReader 和 BinaryWriter
在读入信息之后,可以对信息进行所需的任何操作。但是,在某些时候,您可能想要将信息写回文件,因此需要 BinaryWriter。在本示例中,您将使用 Seek 方法将信息追加到文件结尾,因此,在开始写入之前,请确保指向文件的指针位于文件结尾。在使用 BinaryWriter 写入信息时有多个选项。因为 Write 方法有足够的重载用于您能够写入的所有信息类型,所以,可以使用 Write 方法向您的编写器封装的流写入任何标准形式的信息。本情况下,还可以使用 WriteString 方法向流中写入长度预先固定的字符串。
VB Source: VB\ReadWrite.aspx
%@ Import Namespace="System.Text" %
%@ Import Namespace="System.IO" %
script language="VB" runat=server
Class TestBinary
Public Shared Function ReadFile(selection As String) As String
Dim output As StringBuilder = New StringBuilder()
Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate)
Dim r As BinaryReader = New BinaryReader(fs)
Try
r.BaseStream.Seek(0,SeekOrigin.Begin) ' 将文件指针设置到文件开始
' 因为不同数据类型之间的很多转换结果都是不可解释的,
' 所以当在其他类型与二进制数据之间进行转换时,
' 必须捕捉可能引发的任何潜在的异常...
' 能够正确读取数据依赖于如何写入信息...
' 这与写日志文件时不同。
Do While r.BaseStream.Position r.BaseStream.Length ' 当未到达文件结尾时
Select Case selection
Case "Boolean"
output.Append( r.ReadBoolean().ToString() )
Case "String"
output.Append( r.ReadString() )
Case "Integer"
output.Append( r.ReadInt32().ToString() )
End Select
Loop
Finally
fs.Close()
End Try
return output.ToString()
End Function
Public Shared Function WriteFile(output As Object, selection As String) As String
Dim fs As FileStream = New FileStream("data.bin", FileMode.Create)
Dim w As BinaryWriter = New BinaryWriter(fs)
Dim strOutput As String = ""
w.BaseStream.Seek(0, SeekOrigin.End) ' 将文件指针设置到文件结尾
' 因为正在写的信息可能不适合于所选择用于写入的特定样式
' (例如,单词“Hello”作为整数?),所以我们必须捕捉写入
' 错误,并通知用户未能执行该任务
Try
Select Case selection
Case "Boolean"
Dim b As Boolean = Convert.ToBoolean(output)
w.Write( b )
Case "String"
Dim s As String = Convert.ToString(output)
w.Write( s )
Case "Integer"
Dim i As Int32 = Convert.ToInt32(output)
w.Write(i)
End Select
Catch E As Exception
' 让用户知道未能写入该信息
strOutput = "写异常:" chr(13) _
"无法以所请求的格式写入要写入的信息。" _
chr(13) "请输入尝试写入的数据类型的有效值"
End Try
fs.Close()
return strOutput
End Function
End Class
Sub btnAction_Click(src As Object, E As EventArgs)
Dim s As String = ""
' 写出文件
s = TestBinary.WriteFile(txtInput.Text, lstDataIn.SelectedItem.Text)
If s = "" Then
Try
' 读回信息,显示信息...
txtOutput.Text = TestBinary.ReadFile(lstDataIn.SelectedItem.Text)
Catch Exc As Exception
' 让用户知道未能写入信息
s = "读异常:" chr(13) _
"无法以所请求的格式读取要写入的信息。" _
chr(13) "请输入尝试写入的数据类型的有效值"
End Try
Else
txtOutput.Text = s
End If
End Sub
/script
html
head
link rel="stylesheet" href="intro.css"
/head
body style="background-color:f6e4c6"
form method=post runat="server"
p
table
tr
tdb
下面的示例使用 BinaryWriter 对象创建一个二进制文件,然后使用 BinaryReader 读取该信息。/b可以选择不同的对象来将所需的信息写入文件
此演示用于强调您需要知道如何读取已写入的二进制文件。一旦以某种格式写入数据,就只能以该格式读取该信息。但是,可以将多种不同的数据类型写入文件。在此演示中,输入任意字符串并将它们作为字符串读取,对于整型,仅输入整型数值项(试试浮点数字,然后看看会发生什么...);对于布尔型项,仅输入词“false”和“true”。
p
hr
/td
/tr
/table
asp:Table id="basetable" runat="server" border="0" cellspacing="0" cellpadding="5"
asp:tablerow
asp:tablecell verticalalign="top"
请选择要保存到二进制文件的数据类型...
/asp:tablecell
asp:tablecell verticalalign="top"
asp:listbox id="lstDataIn" runat="server"
asp:listitemBoolean/asp:listitem
asp:listitem selected="true"String/asp:listitem
asp:listitemInteger/asp:listitem
/asp:listbox
/asp:tablecell
asp:tablecell verticalalign="top"
asp:button id="btnAction" onclick="btnAction_Click" Text="写入/读取文件" runat="server"/
/asp:tablecell
/asp:tablerow
Public Class SimpleCalculator
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
End Sub
'窗体重写处置以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer
'注意:以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents Button4 As System.Windows.Forms.Button
Friend WithEvents Button5 As System.Windows.Forms.Button
Friend WithEvents Button6 As System.Windows.Forms.Button
Friend WithEvents Button7 As System.Windows.Forms.Button
Friend WithEvents Button8 As System.Windows.Forms.Button
Friend WithEvents Button9 As System.Windows.Forms.Button
Friend WithEvents Button10 As System.Windows.Forms.Button
Friend WithEvents Button11 As System.Windows.Forms.Button
Friend WithEvents Button12 As System.Windows.Forms.Button
Friend WithEvents Button13 As System.Windows.Forms.Button
Friend WithEvents Button14 As System.Windows.Forms.Button
Friend WithEvents Button15 As System.Windows.Forms.Button
Friend WithEvents Button16 As System.Windows.Forms.Button
System.Diagnostics.DebuggerStepThrough() Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.Button4 = New System.Windows.Forms.Button
Me.Button5 = New System.Windows.Forms.Button
Me.Button6 = New System.Windows.Forms.Button
Me.Button7 = New System.Windows.Forms.Button
Me.Button8 = New System.Windows.Forms.Button
Me.Button9 = New System.Windows.Forms.Button
Me.Button10 = New System.Windows.Forms.Button
Me.Button11 = New System.Windows.Forms.Button
Me.Button12 = New System.Windows.Forms.Button
Me.Button13 = New System.Windows.Forms.Button
Me.Button14 = New System.Windows.Forms.Button
Me.Button15 = New System.Windows.Forms.Button
Me.Button16 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(48, 16)
Me.Label1.TabIndex = 0
Me.Label1.Text = "结果:"
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(64, 8)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(120, 21)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(200, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(56, 24)
Me.Button1.TabIndex = 2
Me.Button1.Text = "清空"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(48, 56)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(24, 24)
Me.Button2.TabIndex = 3
Me.Button2.Text = "1"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(88, 56)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(24, 24)
Me.Button3.TabIndex = 4
Me.Button3.Text = "2"
'
'Button4
'
Me.Button4.Location = New System.Drawing.Point(136, 56)
Me.Button4.Name = "Button4"
Me.Button4.Size = New System.Drawing.Size(24, 24)
Me.Button4.TabIndex = 5
Me.Button4.Text = "3"
'
'Button5
'
Me.Button5.Location = New System.Drawing.Point(48, 88)
Me.Button5.Name = "Button5"
Me.Button5.Size = New System.Drawing.Size(24, 24)
Me.Button5.TabIndex = 6
Me.Button5.Text = "4"
'
'Button6
'
Me.Button6.Location = New System.Drawing.Point(88, 88)
Me.Button6.Name = "Button6"
Me.Button6.Size = New System.Drawing.Size(24, 24)
Me.Button6.TabIndex = 7
Me.Button6.Text = "5"
'
'Button7
'
Me.Button7.Location = New System.Drawing.Point(136, 88)
Me.Button7.Name = "Button7"
Me.Button7.Size = New System.Drawing.Size(24, 24)
Me.Button7.TabIndex = 8
Me.Button7.Text = "6"
'
'Button8
'
Me.Button8.Location = New System.Drawing.Point(48, 120)
Me.Button8.Name = "Button8"
Me.Button8.Size = New System.Drawing.Size(24, 24)
Me.Button8.TabIndex = 9
Me.Button8.Text = "7"
'
'Button9
'
Me.Button9.Location = New System.Drawing.Point(88, 120)
Me.Button9.Name = "Button9"
Me.Button9.Size = New System.Drawing.Size(24, 24)
Me.Button9.TabIndex = 10
Me.Button9.Text = "8"
'
'Button10
'
Me.Button10.Location = New System.Drawing.Point(136, 120)
Me.Button10.Name = "Button10"
Me.Button10.Size = New System.Drawing.Size(24, 24)
Me.Button10.TabIndex = 11
Me.Button10.Text = "9"
'
'Button11
'
Me.Button11.Location = New System.Drawing.Point(48, 152)
Me.Button11.Name = "Button11"
Me.Button11.Size = New System.Drawing.Size(24, 24)
Me.Button11.TabIndex = 12
Me.Button11.Text = "0"
'
'Button12
'
Me.Button12.Location = New System.Drawing.Point(176, 56)
Me.Button12.Name = "Button12"
Me.Button12.Size = New System.Drawing.Size(24, 24)
Me.Button12.TabIndex = 13
Me.Button12.Text = "+"
'
'Button13
'
Me.Button13.Location = New System.Drawing.Point(176, 88)
Me.Button13.Name = "Button13"
Me.Button13.Size = New System.Drawing.Size(24, 24)
Me.Button13.TabIndex = 14
Me.Button13.Text = "-"
'
'Button14
'
Me.Button14.Location = New System.Drawing.Point(176, 120)
Me.Button14.Name = "Button14"
Me.Button14.Size = New System.Drawing.Size(24, 24)
Me.Button14.TabIndex = 15
Me.Button14.Text = "*"
'
'Button15
'
Me.Button15.Location = New System.Drawing.Point(176, 152)
Me.Button15.Name = "Button15"
Me.Button15.Size = New System.Drawing.Size(24, 24)
Me.Button15.TabIndex = 16
Me.Button15.Text = "/"
'
'Button16
'
Me.Button16.Location = New System.Drawing.Point(88, 152)
Me.Button16.Name = "Button16"
Me.Button16.Size = New System.Drawing.Size(72, 24)
Me.Button16.TabIndex = 17
Me.Button16.Text = "计算"
'
'SimpleCalculator
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(264, 190)
Me.Controls.Add(Me.Button16)
Me.Controls.Add(Me.Button15)
Me.Controls.Add(Me.Button14)
Me.Controls.Add(Me.Button13)
Me.Controls.Add(Me.Button12)
Me.Controls.Add(Me.Button11)
Me.Controls.Add(Me.Button10)
Me.Controls.Add(Me.Button9)
Me.Controls.Add(Me.Button8)
Me.Controls.Add(Me.Button7)
Me.Controls.Add(Me.Button6)
Me.Controls.Add(Me.Button5)
Me.Controls.Add(Me.Button4)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Name = "SimpleCalculator"
Me.Text = "简单计算器"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub TextBox1_TabStopChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim btn As Button '按钮类型的变量
btn = sender '把产生该事件的按钮对象赋值给btn
TextBox1.Text = TextBox1.Text + btn.Text '把该按钮的Text属性值连接到TextBox1中
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
Dim btn As Button
btn = sender
TextBox1.Text += btn.Text
End Sub
Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
Dim btn As Button
btn = sender
'在文本框的Text属性后连接一个空格、本按钮的Text属性值和一个空格
TextBox1.Text = TextBox1.Text + " " + btn.Text + " "
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "" '清空文本框
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
Dim btn As Button
btn = sender
TextBox1.Text = TextBox1.Text + " " + btn.Text + " "
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
Dim btn As Button
btn = sender
TextBox1.Text = TextBox1.Text + " " + btn.Text + " "
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
Dim btn As Button
btn = sender
TextBox1.Text = TextBox1.Text + " " + btn.Text + " "
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
'发生错误转移到标号“ErrorPro”指定的行去执行错误处理程序
On Error GoTo ErrorPro
Dim r As Decimal '保存计算结果的变量
Dim t As String = TextBox1.Text '用于保存文本框中的算术表达式
Dim space As Integer = t.IndexOf(" ") '搜索空格位置,如果没有空格,返回值为0
'字符串的取子符方法,第二个参数小于0,则将产生错误号为5的异常,即过程参数不正确
Dim s1 As String = t.Substring(0, space) '通过取子串方法获得第一个运算数
Dim op As String = t.Substring(space + 1, 1) '通过取子串方法获得运算符
Dim s2 As String = t.Substring(space + 3) '通过取子串方法获得第二个运算数
Dim arg1, arg2 As Integer
arg1 = Val(s1) : arg2 = Val(s2)
Select Case op
Case "+" : r = arg1 + arg2
Case "-" : r = arg1 - arg2
Case "*" : r = arg1 * arg2
Case "/" : r = arg1 / arg2
Case Else
MsgBox("输入的运算符有误!")
Exit Sub
End Select
TextBox1.Text = CStr(r) '显示结果
Exit Sub '退出过程
ErrorPro: '错误处理程序块
Select Case Err.Number
Case 6 '除数为零时,或运算溢出时的错误号
MsgBox("算术运算溢出!", , "溢出提示")
TextBox1.Focus()
Exit Sub
Case 5 ' Substring过程的参数不符合要求的错误号
MsgBox("必须输入运算符和第二个运算数!", , "运算数少")
Exit Sub
Case Else
'其它情况显示错误号和错误原因
MsgBox("错误号为" Err.Number Chr(10) Chr(13) "错误原因:" Err.Description)
Exit Sub
End Select
End Sub
End Class
首先引入System.IO命名空间
Imports System.IO
然后使用文件流来读入数组:
Dim bytes() As Byte
Using fs As New FileStream(文件路径,FileMode.Open)
ReDim bytes(fs.Length-1)
fs.Read(bytes,0,fs.Length)
fs.Close()
End Using
这样bytes就是整个文件的所有字节了
从字节生成Image:
Dim img As Image = Image.FromStream(New MemoryStream(bytes))
img就是图片了
VB.NET打开二进制文件用fileopen完成,打开二进制文件的形式为:openmode.binary
读取二进制文件用的是fileget方法,写入二进制文件用的是fileput方法。
应用示例:将一批随机数保存在一个dat文件中,然后再将其提取到文本框中。
二进制文件的读写一批随机数的存取,程序为:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x, i, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
For i = 1 To 8
x = Int(Rnd() * 100)
s = s + Str(x)
FilePut(fn, x)
Next
FileClose(fn)
TextBox1.Text = s
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
Do While Not EOF(fn)
FileGet(fn, x)
s = s + Str(x) + " "
Loop
FileClose(fn)
TextBox1.Text = s
End Sub
open函数用来打开一个文件,其调用的一般形式为:
文件指针名=fopen(文件名,使用文件方式);
其中,
“文件指针名”必须是被说明为FILE
类型的指针变量;
“文件名”是被打开文件的文件名;
“使用文件方式”是指文件的类型和操作要求。
“文件名”是字符串常量或字符串数组。
例如:
FILE
*fp;
fp=("file
a","r");
其意义是在当前目录下打开文件file
a,只允许进行“读”操作,并使fp指向该文件。
又如:
FILE
*fphzk
fphzk=("c:\\hzk16","rb")
其意义是打开C驱动器磁盘的根目录下的文件hzk16,这是一个二进制文件,只允许按二进制方式进行读操作。两个反斜线“\\
”中的第一个表示转义字符,第二个表示根目录。
使用文件的方式共有12种,下面给出了它们的符号和意义。
文件使用方式
意义
“rt”
只读打开一个文本文件,只允许读数据
“wt”
只写打开或建立一个文本文件,只允许写数据
“at”
追加打开一个文本文件,并在文件末尾写数据
“rb”
只读打开一个二进制文件,只允许读数据
“wb”
只写打开或建立一个二进制文件,只允许写数据
“ab”
追加打开一个二进制文件,并在文件末尾写数据
“rt+”
读写打开一个文本文件,允许读和写
“wt+”
读写打开或建立一个文本文件,允许读写
“at+”
读写打开一个文本文件,允许读,或在文件末追加数据
“rb+”
读写打开一个二进制文件,允许读和写
“wb+”
读写打开或建立一个二进制文件,允许读和写
“ab+”
读写打开一个二进制文件,允许读,或在文件末追加数据
对于文件使用方式有以下几点说明:
1)
文件使用方式由r,w,a,t,b,+六个字符拼成,各字符的含义是:
r(read):
读
w(write):
写
a(append):
追加
t(text):
文本文件,可省略不写
b(banary):
二进制文件
+:
读和写
2)
凡用“r”打开一个文件时,该文件必须已经存在,且只能从该文件读出。
3)
用“w”打开的文件只能向该文件写入。若打开的文件不存在,则以指定的文件名建立该文件,若打开的文件已经存在,则将该文件删去,重建一个新文件。
4)
若要向一个已存在的文件追加新的信息,只能用“a”方式打开文件。但此时该文件必须是存在的,否则将会出错。
5)
在打开一个文件时,如果出错,fopen将返回一个空指针值NULL。在程序中可以用这一信息来判别是否完成打开文件的工作,并作相应的处理。因此常用以下程序段打开文件:
6)
if((fp=fopen("c:\\hzk16","rb")==NULL)
{
printf("\nerror
on
open
c:\\hzk16
file