{the data structure for our information}
Information = record
Name: array[0..255] of char;
Title: array[0..255] of char;
Age: Integer;
end;var
Form1: TForm1;implementationprocedure TForm1.Button1Click(Sender: TObject);
var
FileHandle: THandle; // a handle to the opened file
TheInfo: Information; // holds our information NumBytesWritten: DWORD; // variable to track bytes written
Security: TSecurityAttributes; // opened file security attributes
begin
{copy the supplied information to the data structure}
StrPCopy(TheInfo.Name, Edit1.Text);
StrPCopy(TheInfo.Title, Edit2.Text);
TheInfo.Age := StrToInt(Edit3.Text); {create a generic, binary file}
Security.nLength := SizeOf(TSecurityAttributes);
Security.bInheritHandle := FALSE; FileHandle := CreateFile('TempFile.nfo', GENERIC_WRITE, 0, @Security,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_SEQUENTIAL_SCAN, 0); {write the data in the data structure directly to the file}
WriteFile(FileHandle, TheInfo, SizeOf(Information), NumBytesWritten, nil); {implicitly set the end of the file. this could be used to set
the end of the file to somewhere in the middle} SetEndOfFile(FileHandle); {force any cached file buffers to write the file to disk}
FlushFileBuffers(FileHandle); {close the file}
CloseHandle(FileHandle);
end;procedure TForm1.Button2Click(Sender: TObject);
var
FileHandle: THandle; // a handle to the opened file
TheInfo: Information; // holds our information
NumBytesRead: DWORD; // holds the number of bytes read Security: TSecurityAttributes; // opened file security attributes
TheTitle: array[0..255] of char; // holds a title string
begin
{open the existing file for reading}
Security.nLength := SizeOf(TSecurityAttributes);
Security.bInheritHandle := FALSE;
FileHandle := CreateFile('TempFile.nfo', GENERIC_READ, 0, @Security,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_SEQUENTIAL_SCAN, 0); {indicate an error if the file does not exist}
if FileHandle=INVALID_HANDLE_VALUE then
begin
ShowMessage('No file exists yet. Press the ''Write to file'' button to '+
'create a file.');
Exit;
end; {lock the entire file so no other process may use it}
LockFile(FileHandle, 0, 0, SizeOf(Information), 0); {read in a block of information and store it in our data structure} ReadFile(FileHandle, TheInfo, SizeOf(Information), NumBytesRead, nil); {display the information}
Label7.Caption := TheInfo.Name;
Label8.Caption := TheInfo.Title;
Label9.Caption := IntToStr(TheInfo.Age); {the title is located 256 bytes into the file from the beginning (this is
how long the Name string is), so reposition the file pointer for reading}
SetFilePointer(FileHandle, SizeOf(TheInfo.Name), nil, FILE_BEGIN); {read one particular string from the file} ReadFile(FileHandle, TheTitle, SizeOf(TheTitle), NumBytesRead, nil); {display the string}
Label11.Caption := TheTitle; {unlock and close the file}
UnlockFile(FileHandle, 0, 0, SizeOf(Information), 0);
CloseHandle(FileHandle);
end;
Information = record
Name: array[0..255] of char;
Title: array[0..255] of char;
Age: Integer;
end;var
Form1: TForm1;implementationprocedure TForm1.Button1Click(Sender: TObject);
var
FileHandle: THandle; // a handle to the opened file
TheInfo: Information; // holds our information NumBytesWritten: DWORD; // variable to track bytes written
Security: TSecurityAttributes; // opened file security attributes
begin
{copy the supplied information to the data structure}
StrPCopy(TheInfo.Name, Edit1.Text);
StrPCopy(TheInfo.Title, Edit2.Text);
TheInfo.Age := StrToInt(Edit3.Text); {create a generic, binary file}
Security.nLength := SizeOf(TSecurityAttributes);
Security.bInheritHandle := FALSE; FileHandle := CreateFile('TempFile.nfo', GENERIC_WRITE, 0, @Security,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_SEQUENTIAL_SCAN, 0); {write the data in the data structure directly to the file}
WriteFile(FileHandle, TheInfo, SizeOf(Information), NumBytesWritten, nil); {implicitly set the end of the file. this could be used to set
the end of the file to somewhere in the middle} SetEndOfFile(FileHandle); {force any cached file buffers to write the file to disk}
FlushFileBuffers(FileHandle); {close the file}
CloseHandle(FileHandle);
end;procedure TForm1.Button2Click(Sender: TObject);
var
FileHandle: THandle; // a handle to the opened file
TheInfo: Information; // holds our information
NumBytesRead: DWORD; // holds the number of bytes read Security: TSecurityAttributes; // opened file security attributes
TheTitle: array[0..255] of char; // holds a title string
begin
{open the existing file for reading}
Security.nLength := SizeOf(TSecurityAttributes);
Security.bInheritHandle := FALSE;
FileHandle := CreateFile('TempFile.nfo', GENERIC_READ, 0, @Security,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or
FILE_FLAG_SEQUENTIAL_SCAN, 0); {indicate an error if the file does not exist}
if FileHandle=INVALID_HANDLE_VALUE then
begin
ShowMessage('No file exists yet. Press the ''Write to file'' button to '+
'create a file.');
Exit;
end; {lock the entire file so no other process may use it}
LockFile(FileHandle, 0, 0, SizeOf(Information), 0); {read in a block of information and store it in our data structure} ReadFile(FileHandle, TheInfo, SizeOf(Information), NumBytesRead, nil); {display the information}
Label7.Caption := TheInfo.Name;
Label8.Caption := TheInfo.Title;
Label9.Caption := IntToStr(TheInfo.Age); {the title is located 256 bytes into the file from the beginning (this is
how long the Name string is), so reposition the file pointer for reading}
SetFilePointer(FileHandle, SizeOf(TheInfo.Name), nil, FILE_BEGIN); {read one particular string from the file} ReadFile(FileHandle, TheTitle, SizeOf(TheTitle), NumBytesRead, nil); {display the string}
Label11.Caption := TheTitle; {unlock and close the file}
UnlockFile(FileHandle, 0, 0, SizeOf(Information), 0);
CloseHandle(FileHandle);
end;
解决方案 »
- DELPHI新手求教个很基础的问题----一直没弄懂----------麻烦大家帮下!!!
- 求普及型DELPHI教程!
- 使用SetForeGroundWindow后,窗口不能最小化,如何处理?
- Delphi的真正对象在哪里?欢迎讨论!!!!
- 如何判断一个字串是否在另一个字串里?
- 告知一下100分送上:)
- 怎样在DBLookUpComboBox中设置一打开就有的默认选项
- 如何在TAdoquery中动态使用带参数的SQL语句?
- 用sql语言的一个小问题
- 调查:骂一骂大陆写Delphi书的无知作者!
- 如何实现:在delphi程序中如何判断是否连接到服务器
- 那为高手知道在delphi中怎么修补损坏的窗体
var
Readed:Cardinal ;
Temp:array [0..256] of Char;
BufferToRead:Cardinal;
begin
Buffer:='';
BufferToRead:=1;
FillChar(Temp,256,0);
ReadFile(FHandle,Temp,1,Readed,nil) ;
FIsAtEndOfFile:=Readed=0;
while Temp[0]<>#13 do
Begin
FIsAtEndOfFile:=Readed=0;
if Readed=0 then Exit;
if Temp[0]<>#10 then Buffer:=Buffer+Temp[0];
ReadFile(FHandle,Temp,BufferToRead,Readed,nil);
end;
end;
// 对应我的整个类
Unit MyFileStream;
interface
uses Windows,SysUtils,Dialogs;
type
TMyFileStream=class
private
FFileName: String;
FHandle: THandle;
FSize: DWord;
FIsAtEndOfFile: bool;
procedure SetFileName(const Value: String);
published
property FileName:String read FFileName write SetFileName;
property Handle:THandle read FHandle;
property Size:DWord read FSize ;
property IsAtEndOfFile:bool read FIsAtEndOfFile ;
public
constructor Create(_FileName:String;OverWrite:bool=true);
destructor Destroy;override;
procedure Write(Buffer:Pointer;Count:Integer);
procedure WriteLine(Buffer:Pointer;Count:Integer);
procedure WriteString(var Buffer:String);
procedure ReadString(var Buffer:String);
procedure GoToBegin;
procedure GoToEnd;
procedure Move(DistanceToMove:Integer);
procedure Close;
function Open(_FileName:String;OverWrite:bool=true):bool;
end;
implementationprocedure TMyFileStream.Close;
begin
CloseHandle(FHandle);
end;constructor TMyFileStream.Create(_FileName: String;OverWrite:bool=true);
begin
FFileName:=_Filename;
if OverWrite then
Begin
DeleteFile(_FileName);
FHandle:=CreateFile(PChar(_FileName),GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ,nil,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL,0)
end
else
FHandle:=CreateFile(PChar(_FileName),GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if FHandle=INVALID_HANDLE_VALUE then Raise(Exception.Create(Format('File create/open error!Error Code is %d.',[GetLastError])));
FSize:=GetFileSize(FHandle,nil);
FIsAtEndOfFile:=false;
end;destructor TMyFileStream.Destroy;
begin
CloseHandle(FHandle);
inherited;
end;procedure TMyFileStream.GoToBegin;
begin
SetFilePointer(FHandle,0,nil,FILE_BEGIN);
end;procedure TMyFileStream.GoToEnd;
begin
SetFilePointer(FHandle,0,nil,FILE_END);
end;procedure TMyFileStream.Move(DistanceToMove: Integer);
begin
SetFilePointer(FHandle,DistanceToMove,nil,FILE_CURRENT);
end;function TMyFileStream.Open(_FileName: String;OverWrite:bool=true): bool;
begin
Close;
FFileName:=_FileName;
Result:=true;
if OverWrite then
begin
DeleteFile(_Filename);
FHandle:=CreateFile(PChar(_FileName),GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ,nil,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL,0)
end
else
FHandle:=CreateFile(PChar(_FileName),GENERIC_READ or GENERIC_WRITE,
FILE_SHARE_READ,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if FHandle=INVALID_HANDLE_VALUE then Raise(Exception.Create(Format('File create/open error!Error Code is %d.',[GetLastError])));
FSize:=GetFileSize(FHandle,nil);
if FHandle=0 then Result:=false;
FIsAtEndOfFile:=false;
end;procedure TMyFileStream.ReadString(var Buffer: String);
var
Readed:Cardinal ;
Temp:array [0..256] of Char;
BufferToRead:Cardinal;
begin
Buffer:='';
BufferToRead:=1;
FillChar(Temp,256,0);
ReadFile(FHandle,Temp,1,Readed,nil) ;
FIsAtEndOfFile:=Readed=0;
while Temp[0]<>#13 do
Begin
FIsAtEndOfFile:=Readed=0;
if Readed=0 then Exit;
if Temp[0]<>#10 then Buffer:=Buffer+Temp[0];
ReadFile(FHandle,Temp,BufferToRead,Readed,nil);
end;
end;procedure TMyFileStream.SetFileName(const Value: String);
begin
FFileName := Value;
Open(FFileName,true);
end;procedure TMyFileStream.Write(Buffer: Pointer; Count: Integer);
var
Temp:array[0..255] of Char;
I:Integer;
begin
for I:=0 to StrLen(PChar(Buffer))-1 do
Temp[I]:=Pchar(Buffer)[i];
FileWrite(FHandle ,Temp,Count);
FSize:=GetFileSize(FHandle,nil);
end;procedure TMyFileStream.WriteLine(Buffer: Pointer; Count: Integer);
var
Temp:Array[0..255] of Char;
I:Integer;
begin
for I:=0 To Count-1 do
Temp[I]:=PChar(Buffer)[i];
Temp[Count]:=#13;
Temp[Count+1]:=#10;
self.Write(@Temp[0],Count+2);
end;procedure TMyFileStream.WriteString(var Buffer: String);
begin
self.WriteLine(PChar(Buffer),StrLen(PChar(Buffer)));
end;end.