此处所列的文章均是我自己从国外的网站摘抄并翻译的,由于英文水平有限,里面肯定有不少错漏.翻译这些东西没有其他的什么用途,只是提高自己的英语阅读能力和编程技术水平而已     

文件和流的一些摘录

读文件和写文件函数。返回值是Bool类型。创建文件。

constructor Create(AHandle: Integer);

constructor THandleStream.Create(AHandle: Integer);
begin
  inherited Create;
  FHandle := AHandle;
end;

//直接调用的文件读。

function FileRead(Handle: Integer; var Buffer; Count: LongWord): Integer;
begin
{$IFDEF MSWINDOWS}

//Identifies the file to be read. The file handle must have been created with GENERIC_READ access to the file. 将要读取文件的标志符,文件句柄必须使用GENERIC_READ存取文件。
  if not ReadFile(THandle(Handle), Buffer, Count, LongWord(Result), nil) then
    Result := -1;
{$ENDIF}
{$IFDEF LINUX}
  Result := __read(Handle, Buffer, Count);
{$ENDIF}
end;

 

  WordRec = packed record
    case Integer of
      0: (Lo, Hi: Byte);
      1: (Bytes: array [0..1] of Byte);
  end;

  LongRec = packed record
    case Integer of
      0: (Lo, Hi: Word);
      1: (Words: array [0..1] of Word);
      2: (Bytes: array [0..3] of Byte);
  end;

  Int64Rec = packed record
    case Integer of
      0: (Lo, Hi: Cardinal);
      1: (Cardinals: array [0..1] of Cardinal);
      2: (Words: array [0..3] of Word);
      3: (Bytes: array [0..7] of Byte);
  end;

hFile

Identifies the file to be read. The file handle must have been created with GENERIC_READ access to the file.

Windows NT

 

For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept functions.

对于异步的读操作,hFile可以是任何通过CreateFile的FILE_FLAG_OVERLAPPED标记打开的句柄。或者是一个通过socket 或accept函数返回的套接字句柄.

Windows 95

For asynchronous read operations, hFile can be a communications resource, mailslot, or named pipe handle opened with the FILE_FLAG_OVERLAPPED flag by CreateFile, or a socket handle returned by the socket or accept functions. Windows 95 does not support asynchronous read operations on disk files.

对于异步的读操作,HFile可以是一个交互的资源,邮件槽,或者是CreateFile函数的FILE_FLAG_OVERLAPPED标记创建的命名管句柄,或者是一个通过socket 或accept函数返回的套接字句柄.Windows 95不支持对磁盘文件异步的读操作

 

The SetFilePointer function moves the file pointer of an open file.

指定文件的位置。 

DWORD SetFilePointer(

    HANDLE hFile, // handle of file
    LONG lDistanceToMove, // number of bytes to move file pointer
    PLONG lpDistanceToMoveHigh, // address of high-order word of distance to move 
    DWORD dwMoveMethod  // how to move
   );

移动文件的指针。

The SetEndOfFile function moves the end-of-file (EOF) position for the specified file to the current position of the file pointer.

BOOL SetEndOfFile(

    HANDLE hFile  // handle of file whose EOF is to be set
   ); 
 

Parameters

hFile

Identifies the file to have its EOF position moved. The file handle must have been created with GENERIC_WRITE access to the file.

 

Return Values

If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

This function can be used to truncate or extend a file. If the file is extended, the contents of the file between the old EOF position and the new position are not defined.
If you called CreateFileMapping to create a file-mapping object for hFile, you must first call UnmapViewOfFile to unmap all views and call CloseHandle to close the file-mapping object before you can call SetEndOfFile.

 

//获取流的大小的技巧。

function TStream.GetSize: Int64;
var
  Pos: Int64;
begin
  Pos := Seek(0, soCurrent);//记录流的当前位置
  Result := Seek(0, soEnd); //从流的末尾开始移动0字节的指针
  Seek(Pos, soBeginning);//将流从开始位置移动到以前的位置
end;

 

function TStream.GetPosition: Int64;
begin
  Result := Seek(0, soCurrent);  //获取流的当前位置,通过移动流的指针。
end;

procedure TStream.SetPosition(const Pos: Int64);
begin
  Seek(Pos, soBeginning);
end;

function TStream.GetSize: Int64;
var
  Pos: Int64;
begin
  Pos := Seek(0, soCurrent); /获取流的当前位置,通过移动流的指针。
  Result := Seek(0, soEnd); /获取流的当前位置,通过移动流的指针。
  Seek(Pos, soBeginning);
end;

 

//内存指针

    FMemory: Pointer;
    FSize, FPosition: Longint;

 

    constructor Create(AHandle: Integer);
    function Read(var Buffer; Count: Longint): Longint; override;  //对读文件的封装


    function Write(const Buffer; Count: Longint): Longint; override;//对写文件的封装
    function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; //定位文件指针
    property Handle: Integer read FHandle;

 

function THandleStream.Read(var Buffer; Count: Longint): Longint;
begin
  Result := FileRead(FHandle, Buffer, Count);
  if Result = -1 then Result := 0;
end;

function THandleStream.Write(const Buffer; Count: Longint): Longint;
begin
  Result := FileWrite(FHandle, Buffer, Count);
  if Result = -1 then Result := 0;
end;

function THandleStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
  Result := FileSeek(FHandle, Offset, Ord(Origin));
end;

procedure THandleStream.SetSize(NewSize: Longint);
begin
  SetSize(Int64(NewSize));
end;

     procedure ReadBuffer(var Buffer; Count: Longint);
    procedure WriteBuffer(const Buffer; Count: Longint);

procedure TStream.ReadBuffer(var Buffer; Count: Longint);
begin
  if (Count <> 0) and (Read(Buffer, Count) <> Count) then
    raise EReadError.CreateRes(@SReadError);
end;

procedure TStream.WriteBuffer(const Buffer; Count: Longint);
begin
  if (Count <> 0) and (Write(Buffer, Count) <> Count) then
    raise EWriteError.CreateRes(@SWriteError);
end;

直接对Read, Write的封装。

 

function TStream.CopyFrom(Source: TStream; Count: Int64): Int64;
const
  MaxBufSize = $F000;
var
  BufSize, N: Integer;
  Buffer: PChar;
begin
  if Count = 0 then
  begin
    Source.Position := 0;
    Count := Source.Size;
  end;
  Result := Count;
  if Count > MaxBufSize then BufSize := MaxBufSize else BufSize := Count;
  GetMem(Buffer, BufSize);
  try
    while Count <> 0 do
    begin
      if Count > BufSize then N := BufSize else N := Count;//开辟临时内存,将
      Source.ReadBuffer(Buffer^, N);
      WriteBuffer(Buffer^, N);
      Dec(Count, N);
    end;
  finally
    FreeMem(Buffer, BufSize);
  end;
end;

posted @ 2010-11-12 20:14  AppleAndPear  阅读(316)  评论(0编辑  收藏  举报