如何:讀取二進制文件
下面的代碼示例演示如何從文件中讀取二進制數據。使用了 System.IO 命名空間中的兩個類:FileStream 和 BinaryReader。FileStream 表示實際的文件。BinaryReader 為允許二進制訪問的流提供接口。
下面的代碼示例使用由如何:編寫二進制文件中的代碼創建的稱為 data.bin 的文件。
示例
// binary_read.cpp
// compile with: /clr
#using<system.dll>
using namespace System;
using namespace System::IO;
int main()
{
String^ fileName = "data.bin";
try
{
FileStream^ fs = gcnew FileStream(fileName, FileMode::Open);
BinaryReader^ br = gcnew BinaryReader(fs);
Console::WriteLine("contents of :", fileName);
while (br->BaseStream->Position < br->BaseStream->Length)
Console::WriteLine(br->ReadInt32().ToString());
fs->Close( );
}
catch (Exception^ e)
{
if (dynamic_cast<FileNotFoundException^>(e))
Console::WriteLine("File '' not found", fileName);
else
Console::WriteLine("Exception: ()", e);
return -1;
}
return 0;
}
★★補充★★
手上的壹個VB項目(過程中發現,.Net果然是好啊),需要在壹個ActiveX中實現HTTP下載功能,我是采用InternetreadFile這個API來實現,壹開始的代碼我是這麽寫的
Function GetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
bDoLoop = True
Do While bDoLoop
sReadBuffer = vbNullString
bRet = InternetReadFile(hOpenUrl, sReadBuffer, Len(sReadBuffer), lNumberOfBytesRead)
s = s & Left$(sReadBuffer, lNumberOfBytesRead)
If Not CBool(lNumberOfBytesRead) Then bDoLoop = False
Loop
FileName = "E:\BitSpirit\Torrent\121212.torrent"
F1 = FreeFile
Open FileName For Binary As F1
Put F1, , s
Close F1
If hOpen <> 0 Then InternetCloseHandle (hOpen)
Getreadfile的申明
Public Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
發現其定義ByVal sBuffer As String 看來只能用String了,在網上查找過程中,發現人家用VC寫的程序中這人參數可以是其它的,所以查看了壹下Internetreadfile的原型。發現的確可以, 所以我把internetreadfile的定義修改了壹下,為了通用,我為新的internetreadfile定義了壹個別名。Internetreadfilebyte申明如下:
Public Declare Function InternetReadFileByte Lib "wininet.dll" Alias "InternetReadFile" (ByVal hFile As Long, ByRef sBuffer As Byte, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
試了壹下的確可以,重新修改函數,(在這過程中發現,如果要取到正確數據,還必須取得文件大小。所以增加了HttpQueryInfo的定義)最後完整的函數
Function FileDownload(sUrl As Variant) As Boolean
Dim b(99) As Byte
Dim EndByte() As Byte
Dim s As String
Dim hOpen As Long
Dim hOpenUrl As Long
Dim bDoLoop As Boolean
Dim bRet As Boolean
Dim bbuffer As Byte
Dim sReadBuffer As String
Dim FileName As String
Dim lNumberOfBytesRead As Long
Dim F1 As Integer
Dim strsize As String
Dim size As Long
strsize = String$(1024, " ")
F1 = FreeFile
stTotal = vbNullString
FileName = "E:\BitSpirit\Torrent\121212.torrent"
Open FileName For Binary As F1
hOpen = InternetOpen(scUserAgent, INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
hOpenUrl = InternetOpenUrl(hOpen, sUrl, vbNullString, 0, INTERNET_FLAG_RELOAD, 0)
bDoLoop = True
HttpQueryInfo hOpenUrl, HTTP_QUERY_CONTENT_LENGTH Or HTTP_QUERY_FLAG_NUMBER, ByVal strsize, Len(strsize), 0
size = CLng(Trim(strsize))
For j = 1 To size \ 100
bDoLoop = InternetReadFileByte(hOpenUrl, b(0), 100, lNumberOfBytesRead)
Put F1, , b
If Not CBool(lNumberOfBytesRead) Then Exit For
Next
If size Mod 100 <> 0 Then
tmp = (size Mod 100) - 1
ReDim EndByte(tmp)
bDoLoop = InternetReadFileByte(hOpenUrl, EndByte(0), tmp + 1, lNumberOfBytesRead)
Put F1, , EndByte
End If
If hOpenUrl <> 0 Then InternetCloseHandle (hOpenUrl)
If hOpen <> 0 Then InternetCloseHandle (hOpen)
Close #1
FileDownload = True
End Function
測試了壹下,完全成功:)