我一直在用 Delphi 编写程序,我想做的是使用“保存文件”设置我的游戏。我一直在 Delphi 中这样做,而不是当我把代码带回家时,我只是在使用一个 pascal 编译器,我似乎无法运行我的程序,因为我收到以下错误
Free Pascal Compiler version 2.6.2-8 [2014/01/22] for x86_64
Copyright (c) 1993-2012 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling control.p
control.p(44,12) Error: Identifier not found "CloseFile"
control.p(116,14) Error: Identifier not found "closeFile"
control.p(127,13) Error: Identifier not found "assignFile"
control.p(143,4) Fatal: There were 3 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode (normal if you did not specify a source file to be compiled)
如果这是一个愚蠢的问题,我很抱歉,但我是文件的新手,我真的希望它能工作。以下是我当前的所有代码,以备不时之需,如果草稿混淆了,我们深表歉意,感谢您的帮助。
program Task3;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils;
Type
gameRec = record
name: string[30];
skill: integer;
str: integer;
modif: integer;
sskill: string[3];
sstr: string[3];
smodif: string[3];
sf: integer;
ssf : string[1];
end;
var
gameFile : file of gameRec;
p1, p2 : gameRec;
procedure showStats;
begin
FileMode := fmOpenRead;
Reset(gameFile);
read(gameFile, p1);
read(gameFile, p2);
writeln;
writeln(p1.name, '''s stats');
writeln('Skill: ', p1.skill);
writeln('Strenght: ', p1.str);
writeln('Modifier: ', p1.modif);
writeln;
writeln(p2.name, '''s stats');
writeln('Skill: ', p2.skill);
writeln('Strenght: ', p2.str);
writeln('Modifier: ', p2.modif);
writeln;
CloseFile(gameFile);
end;
procedure resetsf;
var
ran12, ran4, namelen: integer;
namepass: boolean;
name: string;
begin
writeln('No save file detected, generating new stats');
namelen := 0;
namepass := false;
repeat
write('What is player 1''s name: ');
readln(name);
namelen := length(name);
if (namelen > 2) and (namelen < 30) then
begin
p1.name := name;
namepass := true;
end
else
writeln('You name must be between 3 and 30 characters');
until namepass = true;
namepass := false;
repeat
write('What is player 2''s name: ');
readln(name);
namelen := length(name);
if (namelen > 2) and (namelen < 30) then
begin
p2.name := name;
namepass := true;
end
else
writeln('You name must be between 3 and 30 characters');
until namepass = true;
ran12 := random(12) + 1;
ran4 := random(4) + 1;
p1.skill := 10 + (ran12 div ran4);
ran12 := random(12) + 1;
ran4 := random(4) + 1;
p1.str := 10 + (ran12 div ran4);
ran12 := random(12) + 1;
ran4 := random(4) + 1;
p2.skill := 10 + (ran12 div ran4);
ran12 := random(12) + 1;
ran4 := random(4) + 1;
p2.str := 10 + (ran12 div ran4);
reWrite(gameFile);
p1.sskill := inttostr(p1.skill); //debug
p1.sstr := inttostr(p1.str);
p1.smodif := inttostr(p1.modif);
//write(gameFile,p1);
p2.sskill := inttostr(p2.skill);
p2.sstr := inttostr(p2.str);
p2.smodif := inttostr(p2.modif); //debug
write(gameFile,p2);
p1.sf := 1;
p1.ssf := inttostr(p1.sf);
write(gameFile,p1); //debug
closeFile(gameFile);
FileMode := fmOpenRead;
Reset(gameFile);
read(gameFile, p1);
read(gameFile, p2);
end;
begin
assignFile(gameFile, 'N:\gamerec.dat');
randomize;
writeln('Game :)');
writeln('By Sam Collins');
writeln;
FileMode := fmOpenRead;
Reset(gameFile);
read(gameFile, p1);
writeln(p1.sf);
if p1.sf = 0 then
resetsf
else
writeln('Save file detected using old stats');
showStats;
readln;
end.
最佳答案
如果您想要 Delphi 兼容性,请将编译器置于 Delphi 模式,方法是使用 -Sd 编译 或将 {$mode Delphi} 添加到源代码(位于顶部,例如 $apptype 附近)。
然后 closefile() 和 assignfile() 将被接受。默认方言是 turbo pascal。 Lazarus 默认将 FPC 放入 objfpc(这与 delphi 类似)。
Closefile 位于具有系统单元增强功能的单元 (objpas) 中,该单元仅在 Delphi 或 objfpc modi 的范围内。
使用 namespace (SYSTEM.sysutils 而不是 sysutils)也可能很危险。更好地简化为 sysutils。命名空间是 Delphi 扩展,仅在 Delphi XE2 中得到重要使用。
我测试了,去掉了{$R *.res},去掉了system。在 sysutils 和 -Sd 使代码编译之前
关于找不到 Pascal CloseFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26553801/