如何时利用WinAPI函数CreateFile创建一个文件? ( 积分: 35 )

  • 主题发起人 主题发起人 suncheng_hong
  • 开始时间 开始时间
S

suncheng_hong

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大富翁可不可以用CreateFile函数给一段示例代码,建一个文件.<br>我来好好研究研究.
 
各位大富翁可不可以用CreateFile函数给一段示例代码,建一个文件.<br>我来好好研究研究.
 
【3007137】<br>procedure Buttom1Click(Sender: TObject);<br>const<br> &nbsp;bSize = 1024; &nbsp;//read buffer size<br>var<br> &nbsp;hFile, rFile: THandle;<br> &nbsp;f: TextFile;<br> &nbsp;sFile: string;<br> &nbsp;iRead: Integer; &nbsp;//read bytes<br> &nbsp;iSize: Integer; &nbsp;//file size<br> &nbsp;Buffer: array [0..bSize] of Char;<br>begin<br> &nbsp;with TOpenDialog.Create(nil) do<br> &nbsp;begin<br> &nbsp; &nbsp;if Execute then<br> &nbsp; &nbsp; &nbsp;sFile := FileName;<br> &nbsp; &nbsp;Free;<br> &nbsp;end;<br><br> &nbsp;if not FileExists(sFile) then exit;<br><br> &nbsp;hFile := CreateFile(PChar(sFile),<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GENERIC_READ,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_SHARE_READ,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OPEN_EXISTING,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FILE_ATTRIBUTE_READONLY,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );<br><br> &nbsp;iSize := GetFileSize(hFile, @iSize);<br><br> &nbsp;sFile := ChangeFileExt(sFile, '.bak');<br><br> &nbsp;rFile := FileCreate(sFile);<br><br> &nbsp;iRead := bSize;<br><br> &nbsp;while iRead &gt; 0 do<br> &nbsp;begin<br> &nbsp; &nbsp;iRead := bSize;<br> &nbsp; &nbsp;if iSize &lt; bSize then iRead := iSize;<br><br> &nbsp; &nbsp;ZeroMemory(@Buffer, iRead);<br> &nbsp; &nbsp;FileRead(hFile, Buffer, iRead);<br> &nbsp; &nbsp;FileWrite(rFile, Buffer, iRead);<br><br> &nbsp; &nbsp;Dec(iSize, iRead);<br> &nbsp;end;<br><br> &nbsp;CloseHandle(rFile);<br> &nbsp;CloseHandle(hFile);<br>end;
 
接受答案了.
 
后退
顶部