读文件: dim s as string Open "f:\readme.txt" For Input As #1 s = StrConv(InputB(LOF(1), #1), vbUnicode) Close #1 debug.print s
Private Type Person ID As Integer Name As String End TypeSub WriteData() Dim MyRecord As Person Dim recordNumber As Integer
Dim FileNum As Integer FileNum = FreeFile() Open App.Path + "\TestFile.dat" For Random As #FileNum For recordNumber = 1 To 5 Step 1 MyRecord.ID = recordNumber MyRecord.Name = "My Name" & recordNumber
Put #FileNum, recordNumber, MyRecord Next recordNumber Close #FileNum End SubSub ReadData() Dim MyRecord As Person Dim recordNumber As Integer Dim FileNum As Integer
FileNum = FreeFile() Open App.Path + "\TestFile.dat" For Random As #FileNum Me.Text1.Text = "" Dim i As Integer i = 1 Do While Not EOF(1) Seek #FileNum, i Get #FileNum, i, MyRecord
Me.Text1.Text = Me.Text1 + Str(MyRecord.ID) + Chr(13) + Chr(10) Me.Text1.Text = Me.Text1 + MyRecord.Name + Chr(13) + Chr(10) Me.Text1.Text = Me.Text1 + "==================" + Chr(13) + Chr(10) i = i + 1 Loop Close #FileNum End SubPrivate Sub Form_Load() WriteData ReadData End Sub
Dim fso As New FileSystemObject Dim fill As File Dim ts As TextStream
filename = Dialog1.filename Set fill = fso.GetFile(filename) Set ts = fill.OpenAsTextStream(ForReading) Text1.Text = ts.ReadAll Err: Exit Sub ---------------------------------------------------------------
'以下是读取Binary file的程式 Dim Buff() as Byte
Open "d:\csys\8504\ctc" For Binary Access Read As #1 ReDim Buff(267)
Do While Not EOF(1) Get #1, , Buff '每次读268个byte进来 'Call 处理Buff 的Routine Loop Close #1
'以下是写入Binary file的程式 Dim Buff() As Byte Open "c:\tc" For Binary Access Write As #1 ReDim Buff(10) Buff = StrConv("这是一个11", vbFromUnicode) Put #1, , Buff
ReDim Buff(1) Buff(0) = 210 Buff(1) = 70 Put #1, , Buff Close #1 End Sub ---------------------------------------------------------------
Const OFS_MAXPATHNAME = 128 Const OF_CREATE = &H1000 Const OF_READ = &H0 Const OF_WRITE = &H1 Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long Private Type OVERLAPPED Internal As Long InternalHigh As Long offset As Long OffsetHigh As Long hEvent As Long End Type Private Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As Byte End Type
Private Sub Command1_Click() Dim OF As OFSTRUCT Dim hFile As Long Dim flen As Long Dim hfilename As String Dim lnglong As Long Dim buff() As Byte Dim i As Long Dim realnum As Long hfilename = "D:\ms\swinapi12\mc.txt" hFile = OpenFile(hfilename, OF, OF_READ) flen = GetFileSize(hFile, lnglong) ReDim buff(flen - 1) i = ReadFile(hFile, buff(0), flen, realnum, ByVal 0&) CloseHandle hFile Dim s As String s = StrConv(buff, vbUnicode) Text1.Text = s End Sub
写文本 Open "C:\test.txt" For Append As #4 Print #4, Now ) Close #4
用CopyMemory进行数据拷贝 Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)
dim StrTemp as string strTemp = string(256, chr(0)) CopyMemory(strTemp, Buffer(0), len(strTemp))
dim StrTemp as string dim str1 as string strTemp = string(256, chr(0)) CopyMemory ByVal StrPtr(strTemp), ByVal VarPtr(Buffer(0)),len(strTemp)
Public Function Read_Text_File() As ADODB.Recordset Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset Dim conn As ADODB.Connection Set conn = New ADODB.Connection conn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};" & _ "DBQ=" & App.Path & ";", "", "" rs.Open "select * from [test#txt]", conn, adOpenStatic, _ adLockReadOnly, adCmdText Set Read_Text_File = rs Set rs = Nothing Set conn = Nothing End FunctionPrivate Sub cmdReadTXT_Click() Set DataGrid1.DataSource = Read_Text_File End Sub test.txt文件内容Test,Test2 1,1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 111,222
dim s as string
Open "f:\readme.txt" For Input As #1
s = StrConv(InputB(LOF(1), #1), vbUnicode)
Close #1
debug.print s
ID As Integer
Name As String
End TypeSub WriteData()
Dim MyRecord As Person
Dim recordNumber As Integer
Dim FileNum As Integer
FileNum = FreeFile()
Open App.Path + "\TestFile.dat" For Random As #FileNum
For recordNumber = 1 To 5 Step 1
MyRecord.ID = recordNumber
MyRecord.Name = "My Name" & recordNumber
Put #FileNum, recordNumber, MyRecord
Next recordNumber
Close #FileNum
End SubSub ReadData()
Dim MyRecord As Person
Dim recordNumber As Integer
Dim FileNum As Integer
FileNum = FreeFile()
Open App.Path + "\TestFile.dat" For Random As #FileNum Me.Text1.Text = ""
Dim i As Integer
i = 1
Do While Not EOF(1)
Seek #FileNum, i
Get #FileNum, i, MyRecord
Me.Text1.Text = Me.Text1 + Str(MyRecord.ID) + Chr(13) + Chr(10)
Me.Text1.Text = Me.Text1 + MyRecord.Name + Chr(13) + Chr(10)
Me.Text1.Text = Me.Text1 + "==================" + Chr(13) + Chr(10)
i = i + 1
Loop Close #FileNum
End SubPrivate Sub Form_Load()
WriteData
ReadData
End Sub
Dim fill As File
Dim ts As TextStream
Dim filename As String
Dim path As String
Dialog1.CancelError = True
On Error GoTo Err
Dialog1.Filter = "所有文件(*.*) ¦*.* ¦文本文件(*.TXT)) ¦*.TXT"
Dialog1.ShowOpen
filename = Dialog1.filename
Set fill = fso.GetFile(filename)
Set ts = fill.OpenAsTextStream(ForReading)
Text1.Text = ts.ReadAll
Err:
Exit Sub
---------------------------------------------------------------
'以下是读取Binary file的程式
Dim Buff() as Byte
Open "d:\csys\8504\ctc" For Binary Access Read As #1
ReDim Buff(267)
Do While Not EOF(1)
Get #1, , Buff '每次读268个byte进来
'Call 处理Buff 的Routine
Loop
Close #1
'以下是写入Binary file的程式
Dim Buff() As Byte
Open "c:\tc" For Binary Access Write As #1
ReDim Buff(10)
Buff = StrConv("这是一个11", vbFromUnicode)
Put #1, , Buff
ReDim Buff(1)
Buff(0) = 210
Buff(1) = 70
Put #1, , Buff
Close #1
End Sub
---------------------------------------------------------------
//不过我想用API函数:
Createfile,Readfile等实现,该如何做,
我已经把数据读入了数组(byte类型),但不能付值给TEXTBOX,汉字就更不行了
读入了数组后,要用strconv函数转换编码,下面是详细的程序:
窗体:一个按钮,一个textbox(设为多行)
Const OFS_MAXPATHNAME = 128
Const OF_CREATE = &H1000
Const OF_READ = &H0
Const OF_WRITE = &H1
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
Private Type OVERLAPPED
Internal As Long
InternalHigh As Long
offset As Long
OffsetHigh As Long
hEvent As Long
End Type
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Sub Command1_Click()
Dim OF As OFSTRUCT
Dim hFile As Long
Dim flen As Long
Dim hfilename As String
Dim lnglong As Long
Dim buff() As Byte
Dim i As Long
Dim realnum As Long
hfilename = "D:\ms\swinapi12\mc.txt"
hFile = OpenFile(hfilename, OF, OF_READ)
flen = GetFileSize(hFile, lnglong)
ReDim buff(flen - 1)
i = ReadFile(hFile, buff(0), flen, realnum, ByVal 0&)
CloseHandle hFile
Dim s As String
s = StrConv(buff, vbUnicode)
Text1.Text = s
End Sub
Open "C:\test.txt" For Append As #4
Print #4, Now )
Close #4
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal length As Long)
strTemp = string(256, chr(0))
CopyMemory(strTemp, Buffer(0), len(strTemp))
dim str1 as string
strTemp = string(256, chr(0))
CopyMemory ByVal StrPtr(strTemp), ByVal VarPtr(Buffer(0)),len(strTemp)
Public Function Read_Text_File() As ADODB.Recordset
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim conn As ADODB.Connection
Set conn = New ADODB.Connection
conn.Open "DRIVER={Microsoft Text Driver (*.txt; *.csv)};" & _
"DBQ=" & App.Path & ";", "", "" rs.Open "select * from [test#txt]", conn, adOpenStatic, _
adLockReadOnly, adCmdText
Set Read_Text_File = rs
Set rs = Nothing
Set conn = Nothing
End FunctionPrivate Sub cmdReadTXT_Click()
Set DataGrid1.DataSource = Read_Text_File
End Sub test.txt文件内容Test,Test2
1,1
2, 2
3, 3
4, 4
5, 5
6, 6
7, 7
8, 8
9, 9
111,222