Delphi流类 遍历读取流中的所有数据


本文整理自网络,侵删。

 unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Memo1: TMemo;

Memo2: TMemo; {需要添加两个 Memo 用于显示}

Button1: TButton;

procedure Button1Click(Sender: TObject);

procedure FormCreate(Sender: TObject);

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

//先制造一个测试文件

procedure TForm1.FormCreate(Sender: TObject);

var

strList: TStringList;

begin

strList := TStringList.Create;

strList.Add('ABCDEFGHIJKLMNOPQRSTUVWXYZ');

strList.SaveToFile('c:\temp\test.txt');

strList.Free;

end;

procedure TForm1.Button1Click(Sender: TObject);

var

ms: TMemoryStream;

c: Char;

s1,s2: string;

begin

ms := TMemoryStream.Create;

ms.LoadFromFile('c:\temp\test.txt'); {读入内存流}

s1 := '';

s2 := '';

ms.Position := 0; {指针到开始}

while ms.Position < ms.Size do {循环读出}

begin

ms.Read(c,1); {每读出一个字节, 指针会自动移到新的位置}

s1 := s1 + c + ' '; {用文本记录}

s2 := s2 + IntToHex(Byte(c),2) + ' '; {用两位数的十六进制记录}

end;

Memo1.Lines.Text := s1;

Memo2.Lines.Text := s2;

{Memo1 会显示: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z }

{Memo2 会显示: 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 0D 0A}

ms.Free;

end;

end.

相关阅读 >>

Delphi 多重循环

Delphi调试服务程序的两种方法

Delphi中对进程内存进行读写

Delphi 设置richedit的行间距

Delphi 遍历控件

Delphi的unicode与gb2312转转换,汉字unicode转gb2312

Delphi xe5 使用 android 内置函数几个小测试

Delphi 判断是否是0-9数字

Delphi使用idhttp.get('') 造成假死(堵塞),请问线程idhttp怎么才能做到不出错?

Delphi代码直接注入别的进程

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...