一个关于文件流的问题,请大家指点。谢谢!!!源代码如下:(30分)

  • 主题发起人 主题发起人 hj6
  • 开始时间 开始时间
H

hj6

Unregistered / Unconfirmed
GUEST, unregistred user!
unit jiami;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
Memo1: TMemo;
Memo2: TMemo;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

type
Tmystream=class(Tfilestream)
private
fkey:string;
public
constructor create(const filename:string; mode:word);
function read(var buffer;count:longint):longint;override;
function write(const buffer;count:longint):longint;override;
property key:string read fkey write fkey ;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ Tmystream }

constructor Tmystream.create(const filename: string; mode: word);
begin

end;

function Tmystream.read(var buffer; count: Integer): longint;
var
Pbu,Pmy,mykey:pchar;
i,mycount,enc:integer;
begin
getmem(Pmy,count);//Ϊpmy·ÖÅäÄÚ´æ
mykey:=pchar(key);//½«keyת»»ÎªpcharÖ¸Õë
try
mycount:=inherited read(Pmy^,count); //½«ÎļþÄÚÈݶÁÈëpmyÖ¸ÏòÄÚ´æÇø
Pbu:=Pchar(@buffer);//½«bufferת»»ÎªpcharÖ¸Õë
for i:=0 to mycount-1 do//½«keyµÄÿ¸ö×Ö·ûÒÀ´ÎÓëpmyµÄÿ¸ö×Ö·ûÑ­»·Ïà¼õ£¬½á¹û·ÅÈëpbuÖ¸ÏòµÄ±äÁ¿
begin
enc:=(ord(Pmy)-ord(mykey[(i mod length(key))])) mod 256;
Pbu:=chr(enc);
end;
finally
freemem(Pmy,count);
end;
result:=mycount;
end;

function Tmystream.write(const buffer; count: Integer): longint;
var
Pbu,Pmy,mykey:pchar;
i,enc:integer;
begin
getmem(pmy,count); //Ϊpmy·ÖÅäÄÚ´æ
mykey:=pchar(key); //½«keyת»»ÎªpcharÖ¸Õë
try
pbu:=pchar(@buffer); //½«bufferת»»ÎªpcharÖ¸Õë
for i:=0 to count-1 do
//½«keyµÄÿ¸ö×Ö·ûÒÔ´ËÓëbufferµÄÿ¸ö×Ö·ûÑ­»·Ïà¼Ó,½á¹û·ÅÈëpmyÖ¸ÏòµÄÄÚ´æÇø
begin
enc:=(ord(pbu)+ord(mykey[(i mod length(key))])) mod 256;
Pmy:=char(enc);
end;
result:=inherited write(Pmy^,count); //½«pmyÖ¸ÏòµÄÄÚÈÝдÈëÎļþ
finally
freemem(Pmy,count);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
mystr:Tfilestream;
begin
if opendialog1.Execute then
begin
mystr:=Tfilestream.create(opendialog1.filename,fmopenread);
try
memo1.lines.LoadFromStream(mystr);
finally
mystr.Free;
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
encstr:Tmystream;
begin
if opendialog1.Execute then
begin
encstr:=Tmystream.create(opendialog1.filename,fmopenread);
encstr.key:=Edit1.Text;
try
memo2.lines.LoadFromStream(encstr);
finally
encstr.Free;
end;
end;
end;


procedure TForm1.Button3Click(Sender: TObject);
var
encstr:Tmystream;
begin
if savedialog1.Execute then
begin
encstr:=Tmystream.create(savedialog1.filename,fmcreate);
encstr.key:=Edit1.Text;
try
memo1.lines.SaveToStream(encstr);
finally
encstr.Free;
end;
end;
end;

end.

这是从网上看到的一篇文章。但运行时出错不能保存加密后的文件。请大家指教。谢谢。
 
一點看法:可能是你Tmystream.create什麼也沒做,也沒有繼承父的什麼東東。
 
thanks a lot!!!
 
后退
顶部