有关文件操作的问题(100分)

  • 主题发起人 主题发起人 www
  • 开始时间 开始时间
blockread blockwrite?
read write什么?
那个string需要自己编程才能写入磁盘或者读出到record中。
 
改成这样吧:
aTRec =record //TRec不能做变量名
name: array[0..7]of char;
sex: array[0..1]of char;
age: integer;
resumesize:integer;
resume:pointer;
end;
用时为pointer中请空间。
可以先算出一个record的大小再用blockwrite写入,
读就没办法用blockread了。
 
TRec作自己的类型名当然是可以的,不过原来的TRec就不能用了,呵呵。
文件的操作具体我也不大记得了,好象是用数组的方式写进去更容易些。
 
谁能给贴出个读记录、写记录的例子???
这分都是他的了。
 
var
F: TFileStream;
R: TRec;
begin
F := TFileStream.Create(...);
F.Seek(n * SizeOf(TRec), soFrombegin
ning);
//seek the n reocrd
F.Write(R, SizeOf(TRec));
// or F.Read(R, SizeOf(TRec));
R.Free;
end;
 
to zhaoyipeng:如何得到记录的每一项内容呢??
 
//自己动手做了个例子:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
aTRec =record //TRec不能做变量名
name: array[0..7]of char;
sex: array[0..1]of char;
age: integer;
resumesize:integer;
resume:pointer;
end;
TMyfile=file of aTRec;
var
Form1: TForm1;
f1:tmyfile;
f2:file;
buff:aTRec;
implementation
{$R *.DFM}
procedure readrecord(var f:file;var b:atrec);
begin
blockread(f,b.name,7);
blockread(f,b.sex,2);
blockread(f,b.age,sizeof(integer));
blockread(f,b.resumesize,sizeof(integer));
getmem(b.resume,buff.resumesize);
blockread(f,b.resume,b.resumesize);
end;
procedure writerecord(var f:file;b:atrec);
begin
blockwrite(f,b.name,7);
blockwrite(f,b.sex,2);
blockwrite(f,b.age,sizeof(integer));
blockwrite(f,b.resumesize,sizeof(integer));
blockwrite(f,b.resume,b.resumesize);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
assignfile(f2,'test.dat');
rewrite(f2,1);
getmem(buff.resume,10);
pchar(buff.resume):='1234567890';
buff.resumesize:=10;
buff.name:='1234567';
buff.sex:='12';
buff.age:=24;
writerecord(f2,buff);
closefile(f2);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
s:pchar;
begin
assignfile(f2,'test.dat');
reset(f2,1);
readrecord(f2,buff);
closefile(f2);
memo1.lines.add(buff.name);
memo1.lines.add(buff.sex);
memo1.lines.add(inttostr(buff.age));
memo1.lines.add(inttostr(buff.resumesize));
s:=buff.resume;
memo1.lines.add(strpas(s));
end;

end.
 
感谢amo大师!!我调通了!
 
多人接受答案了。
 
虽然讨论已经结束了,因为我用的是string,不是point,所以
我还是把我的贴一下:
TSwissProtRec = record
SeqID: array[0..7]of char;
SeqName:array[0..19]of char;
SeqDate:TDateTime;
SeqContent:array[0..254] of char;
SeqLength:integer;
Sequence:string;
end;

function readrecord(var f:file;var b:TSwissProtRec):boolean;
var
sSequence:PChar;
begin
try
blockread(f,b.SeqID,8);
blockread(f,b.SeqName,20);
blockread(f,b.SeqDate,sizeof(TDateTime));
blockread(f,b.SeqContent,255);
blockread(f,b.SeqLength,sizeof(integer));
getmem(sSequence,b.SeqLength);
blockread(f,sSequence^,b.SeqLength);
b.Sequence := StrPas(sSequence);
FreeMem(sSequence);
result := true;
except
result := false;
end;
end;

procedure writerecord(var f:file;b:TSwissProtRec);
begin
blockwrite(f,b.SeqID,8);
blockwrite(f,b.SeqName,20);
blockwrite(f,b.SeqDate,sizeof(TDateTime));
blockwrite(f,b.SeqContent,255);
blockwrite(f,b.SeqLength,sizeof(integer));
blockwrite(f,PChar(b.Sequence)^,b.SeqLength);
end;

 
高手如云!!!!!
 

Similar threads

回复
0
查看
1K
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部