关于RichEdit与文件流的一个小问题,(30分)

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

hds2008

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是实验源码,
先执行procedure TForm1.Button2Click(Sender: TObject);保存文件后,再从保存的文件Load时
不知道为什么执行 richedit1.Lines.LoadFromStream(rtfstrm) 后,
RichEdit内不见有内容啊,请大侠指点迷津.[:D]


[blue][/blue]
unit Unit1;

interface

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

type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
Button1: TButton;
Button2: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
fileStrm: TFileStream;
rtfStrm: TMemoryStream;
VfsFileName: string;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
rtfStrm := TMemoryStream.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.Execute;
if OpenDialog1.Files.Count = 0 then exit;
VfsFileName := OpenDialog1.FileName;
fileStrm := TFileStream.Create(VfsFileName, fmOpenReadWrite);
filestrm.Position := 0;
rtfStrm.CopyFrom(fileStrm,fileStrm.Size);
richedit1.Lines.LoadFromStream(rtfstrm);
filestrm.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
saveDialog1.Execute;
if saveDialog1.Files.Count = 0 then exit;
VfsFileName := saveDialog1.FileName;
fileStrm := TFileStream.Create(VfsFileName, fmCreate or fmOpenReadWrite);
rtfstrm.Position := 0;
filestrm.CopyFrom(rtfstrm,rtfstrm.Size)
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
rtfstrm.Free;
end;

end.
 
那个大侠第一个帮解决,小弟就所有的积分都送给第一个解决的人[:(]
 
改了一下,功能一样

unit Unit1;

interface

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

type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
Button1: TButton;
Button2: TButton;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
S : TStream;
begin
if OpenDialog1.Execute then
try
S := TFileStream.Create(OpenDialog1.FileName, fmOpenReadWrite);
try
richedit1.Lines.LoadFromStream(S);
finally
S.Free;
end;
except
on E: Exception do
begin
ShowMessage(E.Message);
end;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
S : TStream;
begin
if SaveDialog1.Execute then
try
S := TFileStream.Create(SaveDialog1.FileName, fmCreate);
try
richedit1.Lines.SaveToStream(S);
finally
S.Free;
end;
except
on E: Exception do
begin
ShowMessage(E.Message);
end;
end;
end;

end.
 
To: tseug
我知道可以那样做,但是我自己自定义了一种自定义文件A,文件A里保存的是多个RichEdit的内容,所以那个方法不可以的
 
那就把每一个RichEdit的Text和长度保存到文件或流里,然后根据长度分别读取就可以了...
 
请问版主:这个问题没有人回答,可不可以问别的问题?
 
load之前stream.position:=0
 
将 rtfStrm.CopyFrom(filestrm, filestrm.size) 该为
rtfStrm.LoadFromStream(filestrm)
就可以了
原因还不太清楚,可能是因为两种不同的stream
用两个FileStream CopyFrom 是可以的
 
感谢LGXing,slb_anders,tseug的帮忙,可是还是没有解决,我上面贴的源码中,
出问题的地方是为什么? 请各位高手讲一讲[:)]
真的没有人有办法解决啊?[:(]
 
多人接受答案了。
 

Similar threads

后退
顶部