VS2010 bin檔案讀取
阿新 • • 發佈:2020-10-26
void CDlgUpGrade::read_bin(char path) //path為BIN檔案路徑
{
方法1:
CFile readfile; /定義CFile結構/
readfile.Open(m_strFileName, CFile::modeReadWrite | CFile::typeBinary); /以二進位制讀寫形式開啟檔案/
UINT lenstr,index,rtn=0,len=0;
int iremain=0;
len = readfile.GetLength(); /讀取檔案的長度*/
UCHAR *ucharbuffer = new UCHAR[len](); /*由file讀到buffer、buffer型別為unsigned char*/ readfile.Read(ucharbuffer, len); readfile.Close(); CString strtmp, strresult; for (UINT i = 0; i < len; i++) /*將buffer內容轉換為16進位制的字串*/ { strtmp.format("%.2x",ucharbuffer[i]); } delete ucharbuffer; /*釋放空間*/
方法2:
unsigned long nFileBytes;//用於儲存BIN檔案總位元組數 unsigned char aBinByte[65536];//用於儲存從BIN檔案獲取的資料 FILE *pFile; pFile=fopen(path,"rb"); //_wfopen_s(&pFile,m_strFileName.GetBuffer(),"rb"); //開啟檔案strFilePath是檔案路徑VS2010是UNICODE編碼 定義時注意轉換或者這樣L"xxx" if(pFile == NULL) //判斷檔案是否開啟成功 { return; } fseek(pFile,0,SEEK_END);//將檔案指標設定到檔案末尾處 nFileBytes=ftell(pFile);//獲取檔案指標的位置 也就相當於檔案的大小了 fseek(pFile,0,SEEK_SET);//重新將檔案指標調回檔案開頭 fread(aBinByte, sizeof(unsigned char) , nFileBytes , pFile);//將整個檔案讀取 注意這裡檔案的大小不應超過65536 fclose(pFile);//關閉檔案
}