如何对无类型文件中的数据进行读写? ( 积分: 100 )

  • 主题发起人 主题发起人 3cs
  • 开始时间 开始时间
3

3cs

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个无类型文件,我想对它的前1024个字节进行异或运算,如何通过文件流的方式进行?<br>能否请高手们举一个简单的实例,谢谢了!
 
有一个无类型文件,我想对它的前1024个字节进行异或运算,如何通过文件流的方式进行?<br>能否请高手们举一个简单的实例,谢谢了!
 
var<br> hwd, FSize, hMap: Cardinal;<br> pData: PChar;<br> bu: array[0..1024]of Char;<br> i: integer;<br>begin<br> hwd:= CreateFile('C:/TrustMe.dat', GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE, nil,<br> &nbsp; OPEN_EXISTING, 0, 0);<br> if hwd = INVALID_HANDLE_VALUE then RaiseLastWin32Error<br> else try<br> &nbsp;FSize:= GetFileSize(hwd, nil);<br> &nbsp;hMap:= CreateFileMapping(hwd,nil,PAGE_READONLY,0,FSize,nil);<br> &nbsp;if hMap = 0 then RaiseLastWin32Error<br> &nbsp;else try<br> &nbsp; pData:= MapViewOfFile(hMap, FILE_MAP_READ,0,0,FSize);<br> &nbsp; if pData = nil then RaiseLastWin32Error<br> &nbsp; else try<br> &nbsp; &nbsp;ZeroMemory(@bu[0], SizeOf(bu));<br> &nbsp; &nbsp;MoveMemory(@bu[0], pData, 1024);<br> &nbsp; &nbsp;for i:= 0 to 1024 do<br> &nbsp; &nbsp; bu:= bu xor 1022;<br> &nbsp; finally<br> &nbsp; &nbsp;UnMapViewOfFile(pData);<br> &nbsp; end;<br> &nbsp;finally<br> &nbsp; CloseHandle(hMap);<br> &nbsp;end;<br> finally<br> &nbsp;CloseHandle(hwd);<br> end;<br>end;
 
var<br> hwd, br: Cardinal;<br> bu: array[0..1024]of Char;<br> i: integer;<br>begin<br> hwd:= CreateFile('C:/TrustMe.dat', GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE, nil,<br> &nbsp; OPEN_EXISTING, 0, 0);<br> if hwd = INVALID_HANDLE_VALUE then RaiseLastWin32Error<br> else try<br> &nbsp;ZeroMemory(@bu[0], SizeOf(bu));<br> &nbsp;if ReadFile(hwd, bu, 1024, br, nil) then begin<br> &nbsp; for i:= 0 to 1024 do<br> &nbsp; &nbsp;bu:= bu xor 1022;<br> &nbsp; if Not WriteFile(hwd, bu, 1024, br, nil) then RaiseLastWin32Error;<br> &nbsp;end else RaiseLastWin32Error;<br> finally<br> &nbsp;CloseHandle(hwd);<br> end;<br>end; &nbsp;<br>
 
var a:array[0..1024] of char;<br>FileStream:=TFileStream.create('filename',fmOpenReadWrite );<br>FileStream.seek(0,soFromBeginning);<br>FileStream.read(a,sizeof(a));<br>a:=a xor b;<br>FileStream.seek(0,soFromBeginning);<br>FileStream.write(a,sizeof(a));
 
多人接受答案了。
 
后退
顶部