【问题标题】:Change dynamically variable file of record types in pascal在pascal中动态更改记录类型的变量文件
【发布时间】:2016-04-07 00:25:04
【问题描述】:
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.


我在这里有问题,我应该为每个数据库声明 2 个不同的变量和整个数据库过程操作。
我想使用动态变量来简化我的代码,例如

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; 


使用此代码,我只能为所有数据库声明一个数据库操作,我该怎么做?我正在使用 lazarus IDE。

【问题讨论】:

  • 你想举个例子吗?不明白有无类型文件还是有类型文件,内存之间有什么关系?
  • 如何将非类型文件传递给过程或函数? @Abelisto
  • 与任何其他变量一样,但使用var 修饰符。
  • 我认为世界已经在发展,现在有更好的方法来存储您的数据

标签: file variables record pascal freepascal


【解决方案1】:

如果你想写入一个无类型的文件你可以使用“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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多