//DELPHI 读取文件的方法(可以读取大于4G的文件)
Private FsFile : string; //文件名 FiSize : Int64; //文件大小(字节数) FiCount: Int64; //bit统计的累计值 Var iFile : Integer; High, Low : DWORD; //DWORD为32位整数 FileSize : Int64; begin iFile := FileOpen(FsFile, fmOpenRead or fmShareDenyNone); High:=0; Low :=GetFileSize(iFile, @High); //GetFileSize的使用方法看链接 {https://blog.csdn.net/liangzhao_jay/article/details/72517114} {https://blog.csdn.net/gududanxing/article/details/10209055} FileSize := H SHR 32 +Low; end;
//DELPHI 计算字节的bit位
1)
function GetBitCount(B: Byte): Integer; var I: Integer; begin Result := 0; for I := 0 to 7 do begin Inc(Result, B and 1); B := B shr 1; end; end;
2)
function GetBitCount(B: Byte): Integer; begin Result := (B and 1) + (B and 2) div 2 + (B and 4) div 4 + (B and 8) div 8 + (B and 16) div 16 + (B and 32) div 32 + (B and 64) div 64 + (B and 128) div 128; end;
3)
bits_arr : array[0..255] of Integer = (0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8); function GetBitCount(B: Byte): Integer; begin Result := bits_arr[B]; end;
//DELPHI 统计文件大小方法
const CSize = 1024 * 1024 * 4{4M}; Var iFile: Integer; buf: PByte; I, rst: Integer; while not Terminated do begin rst := FileRead(iFile, buf^, CSize); {FileRead:从指定文件(当前文件指针)中读取相应字节的数据,同时调整文件指针位置。其中, buf用于保存从文件中读取的数据;CSize表示读取的字节数。返回实际读取的字节数。} if rst <= 0 then Break; for I := 0 to rst -1 do begin FiCount := FiCount + GetBitCount(buf[I]); end; end;