在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; Button2: TButton; Button3: TButton; procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} var mstream:TMemoryStream; procedure TForm1.FormCreate(Sender: TObject); //程序开始先创建一个准备要测试的文件 var list:TStringList; begin list:=TStringList.Create; with list do begin Add('aaaaaaaa'); Add('bbbbbbbb'); Add('cccccccc'); Add('dddddddd'); SaveToFile('f:\temp\test.txt'); end; list.Free; mstream:=TMemoryStream.Create; {同时建立内存流} end; procedure TForm1.FormDestroy(Sender: TObject); begin mstream.Free; end; procedure TForm1.Button1Click(Sender: TObject); begin mstream.LoadFromFile('f:\temp\test.txt'); Memo1.Lines.LoadFromStream(mstream); end; procedure TForm1.Button2Click(Sender: TObject); //用字符指针读取流中的内容 var pstr:PChar; begin pstr:=mstream.Memory; {把字符指针指向内存流} ShowMessage(pstr[0]); {a; 第一个字符} ShowMessage(pstr[10]); {b; 这个第二行的第一个字符; 每行8个字再加换行与回车共10个字符} ShowMessage(pstr[20]); {c} ShowMessage(pstr[30]); {d} end; procedure TForm1.Button3Click(Sender: TObject); var buffer:array[0..2] of Char; begin mstream.Seek(0,soFromBeginning); {参数1: Offset 是偏移量;soFromBeginning: 以开始为基准, 此时参数 Offset 要 >= 0; 如果读取的话那就是向后读取} mstream.Read(buffer,SizeOf(buffer)); ShowMessage(buffer); {aaa} mstream.Seek(10,soFromCurrent); //soFromCurrent: 以当前位置为基准; mstream.Read(buffer,SizeOf(buffer)); ShowMessage(buffer); {bbb} mstream.Seek(20,soFromEnd); {soFromEnd: 以结束为基准; 此时参数 Offset 要 <= 0; 如果读取的话那就是向前读取} mstream.Read(buffer,SizeOf(buffer)); ShowMessage(buffer); {bbb} mstream.Seek(30,soFromBeginning); mstream.Read(buffer,SizeOf(buffer)); ShowMessage(buffer); {ddd} end; end. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论