Home > Back-end >  Can the "file" structure as a variable is passed to the procedure
Can the "file" structure as a variable is passed to the procedure

Time:09-18

There are many different types of the type, structure file storage into. Bat file, for example, the following
 
Type
A=the packed record
A1: integer;
A2: integer;
end;

Type
B=the packed record
B1: double;
B2: integer;
end;

Type
C=the packed record
C1: char;
C2: double;
end;

Var
Af: the file of a;
Bf: the file of b;
Cf: the file of c;


Can use a function or process the above different types are saved to a file, namely the af, bf, cf when variable transmission,
If no need to write a lot of duplicate code,

CodePudding user response:

Can be read into the ini file

Structure can also be passed as a parameter

CodePudding user response:

Ini find file too slow,
How to put the af: the file of a; When variable transmission,
A great god,

CodePudding user response:

 
//deal with the same structure in the same file
Type
A=the packed record
A1: integer;
A2: integer;
end;

Type
B=the packed record
B1: double;
B2: integer;
end;

Type
C=the packed record
C1: char;
C2: double;
end;

Var
Form1: TForm1;

Implementation

{$R *. DFM}

Procedure WriteData (AStream: TStream; AValue: Pointer; ASize: Integer);
The begin
AStream. WriteBuffer (AValue ^ ASize);
end;

Procedure TForm1. BtnWriteAClick (Sender: TObject);
Var
I: Integer;
Aa: a;
Fs: TFileStream;
Mode: the Word;
The begin
Mode:=fmCreate;
If FileExists (' c: \ a. d. at ') then mode:=fmOpenWrite;
Fs:=TFileStream. Create (' c: \ a. d. at ', mode).
Try
Fs. Seek (0, soEnd);

For I:=1 to 9 do
The begin
Aa. A1:=I;
Aa. A2:=I * 10 + I;
WriteData (fs, @ aa, sizeof (aa));
end;
The finally
Fs. Free;
end;
end;

Procedure TForm1. BtnReadAClick (Sender: TObject);
Var
Fs: TFileStream;
Aa: a;
I, iC: Integer;
The begin
Fs:=TFileStream. Create (' c: \ a. d. at ', fmOpenRead);
Try
IC:=fs. Size div sizeof (aa);
For I:=1 to iC do
The begin
Fs. ReadBuffer (aa, sizeof (aa));
Showmessage (inttostr (aa. A1) + ', '+ inttostr (aa) a2));
end;
The finally
Fs. Free;
end;
end;

Procedure TForm1. BtnWriteBClick (Sender: TObject);
Var
I: Integer;
Bb: b;
Fs: TFileStream;
Mode: the Word;
The begin
Mode:=fmCreate;
If FileExists (' c: \ b.d at ') then mode:=fmOpenWrite;
Fs:=TFileStream. Create (' c: \ b.d at ', mode).
Try
Fs. Seek (0, soEnd);

For I:=1 to 10 do
The begin
Bb. B1:=I;
Bb. B2:=I * 10 + I;
WriteData (fs, @ bb, sizeof (bb));
end;
The finally
Fs. Free;
end;
end;

CodePudding user response:

http://www.json.org

CodePudding user response:

The
reference 3 floor wdonghai response:
 
//deal with the same structure in the same file
Type
A=the packed record
A1: integer;
A2: integer;
end;

Type
B=the packed record
B1: double;
B2: integer;
end;

Type
C=the packed record
C1: char;
C2: double;
end;

Var
Form1: TForm1;

Implementation

{$R *. DFM}

Procedure WriteData (AStream: TStream; AValue: Pointer; ASize: Integer);
The begin
AStream. WriteBuffer (AValue ^ ASize);
end;

Procedure TForm1. BtnWriteAClick (Sender: TObject);
Var
I: Integer;
Aa: a;
Fs: TFileStream;
Mode: the Word;
The begin
Mode:=fmCreate;
If FileExists (' c: \ a. d. at ') then mode:=fmOpenWrite;
Fs:=TFileStream. Create (' c: \ a. d. at ', mode).
Try
Fs. Seek (0, soEnd);

For I:=1 to 9 do
The begin
Aa. A1:=I;
Aa. A2:=I * 10 + I;
WriteData (fs, @ aa, sizeof (aa));
end;
The finally
Fs. Free;
end;
end;

Procedure TForm1. BtnReadAClick (Sender: TObject);
Var
Fs: TFileStream;
Aa: a;
I, iC: Integer;
The begin
Fs:=TFileStream. Create (' c: \ a. d. at ', fmOpenRead);
Try
IC:=fs. Size div sizeof (aa);
For I:=1 to iC do
The begin
Fs. ReadBuffer (aa, sizeof (aa));
Showmessage (inttostr (aa. A1) + ', '+ inttostr (aa) a2));
end;
The finally
Fs. Free;
end;
end;

Procedure TForm1. BtnWriteBClick (Sender: TObject);
Var
I: Integer;
Bb: b;
Fs: TFileStream;
Mode: the Word;
The begin
Mode:=fmCreate;
If FileExists (' c: \ b.d at ') then mode:=fmOpenWrite;
Fs:=TFileStream. Create (' c: \ b.d at ', mode).
Try
Fs. Seek (0, soEnd);

For I:=1 to 10 do
The begin
Bb. B1:=I;
Bb. B2:=I * 10 + I;
WriteData (fs, @ bb, sizeof (bb));
end;
The finally
Fs. Free;
end;
end;


You this function method can be stored, but export bad data processing
  • Related