Function Loadstr(n:Integer):string;//读标题对应的文章函数
var
meint:integer;
s1
char;
fs:TFileStream;
begin
fs:=TFileStream.Create((Extractfilepath(Application.ExeName)+'Mybook.txt'),fmOpenRead );
try
getmem(s1,fs.size);
fs.Seek(n,soFromBeginning);
fs.Readbuffer (meint,sizeof(meint));
fs.Seek((n+meint+4),soFromBeginning);
fs.Readbuffer(meint,sizeof(meint));
fs.Readbuffer(s1^,meint);
Result:=s1;
freemem(s1);
finally
fs.free;
end;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
If ListBox1.ItemIndex<>-1 Then
Listbox2.ItemIndex :=Listbox1.ItemIndex ;
RichEdit1.Text:=loadstr(strtoint(ListBox2.Items.Strings[ListBox2.ItemIndex]));
end;
Function mywrite(Bstr,Sstr:string):Boolean;//写入流的函数
var
s,s1
char;
meint:integer;
fs:TFileStream;
begin
try
if FileExists((Extractfilepath(Application.ExeName)+'Mybook.txt')) then//判断文件在不在
begin
fs:=TFileStream.Create((Extractfilepath(Application.ExeName)+'Mybook.txt'),fmOpenWrite );
end
else
begin
fs:=TFileStream.Create((Extractfilepath(Application.ExeName)+'Mybook.txt'),fmCreate );
end;
s:=Pchar(Trim(Bstr));
s1:=pchar(Trim(Sstr));
try
fs.Seek(0,soFromEnd);//指针移到文件尾
meint:=length(s);//计算字符串大小
fs.Writebuffer(meint,sizeof(meint));//把大小写入文件
fs.Writebuffer(s^,Length(s));//把字符串写入文件
meint:=length(s1);//计算第二个字符串的大小
fs.Writebuffer(meint,sizeof(meint));//把大小写入文件
fs.Writebuffer(s1^,length(s1));//把第二个字符串写入文件
finally
fs.free;
end;
except
Result:=False;
Exit;
end;
Result:=True;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
if RichEdit1.Text <>'' then//如果文件框中是空将不保存
begin
if Edit1.Text <>'' then
begin
myWrite(Edit1.Text ,RichEdit1.Text );
Button1.Enabled :=false;
Application.MessageBox('保存成功!','信息',MB_ICONINFORMATION+MB_OK);
end;
end
else
Application.MessageBox('文本框中不能为空!','信息',MB_ICONINFORMATION+MB_OK);
end;