简体   繁体   中英

Change dynamically variable file of record types in pascal

database : array [1..3] of string = ('QA.db','Level.db','Highscore.db'); 

type QA = record
 Question : string[255];
 Options  : array['A'..'D'] of string[255];
 Answer   : char;
end;

type level = record
 money: longint;
 safe: boolean;
end;

type score = record
  name : string[255];
  reward : longint;
 end;

var
 f1:file of QA;
 ftemp1: QA;
 f2 : file of level;
 ftemp2: level;
 f3 : file of score;
 ftemp3: score;          

//Database Operation
  procedure print, change, etc.
  begin reset(f1); ....; write(f,ftemp); close(f1); end; etc.


I have problem in here, that i should declare 2 different variable and whole database procedure operation for each database.
I would like to use dynamic variable for simplifying my code, example

var
 f:file of VAR;
 ftemp: VAR;

procedure Add;
begin
 reset(f);
 ....
 write(f,ftemp);
 close(f);
end;

begin
 VAR:QA;
 Add;
 VAR:level;
 Change;
end; 


With this code I could only declare one database operation for all database, how could I do it? I'm using lazarus IDE.

If You want to write to a untyped file You can use "TextFile"

procedure xyz..
var
  myFile: TextFile;
begin
  AssignFile(myFile, 'C:\QA.db');
  try
    rewrite(myFile);
    writeln(myFile, 'New Sample text!');
    CloseFile(myFile);
  except
    // Important ERROR handling!
    on E: EInOutError do
      writeln('Some error in filewriting!: ', E.ClassName, ':', E.Message);
  end;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM