Sam Collins
Sam Collins

Reputation: 483

Pascal CloseFile not found

I have been making a program in Delphi and what I am trying to do is set up me game with a 'save file'. I have been doing this in Delphi and not when I bring the code home I am just using a pascal compiler and I cannot seem to run my program as I get the following errors

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)

Sorry if this is a stupid question but I am new to files and I really want this to work. Below is all of my current code just in case you need it, sorry if its confusing its a draft and thanks for helping.

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.

Upvotes: 2

Views: 3055

Answers (2)

Marco van de Voort
Marco van de Voort

Reputation: 26356

If you want delphi compatibility, put the compiler in Delphi mode, either by compiling with -Sd or adding {$mode Delphi} to the source (somewhere at the top, e.g. near the $apptype).

Then closefile() and assignfile() will be accepted. The default dialect is turbo pascal. Lazarus puts FPC in objfpc (which is also delphi alike) by default.

Closefile is in an unit (objpas) with system unit enhancements that is only in scope in Delphi or objfpc modi.

Using namespaces (SYSTEM.sysutils instead of sysutils) might be dangerous too. Better simplify to sysutils. Namespaces is an Delphi extension that only got significant use with Delphi XE2.

I tested, and removing the {$R *.res}, the removal of system. before sysutils and -Sd makes the code compile

Upvotes: 9

Bogdan Doicin
Bogdan Doicin

Reputation: 2416

If I understood the question correctly, you want to port a piece of code from Delphi to Free Pascal and you have problems with file operations.

In Free (and Turbo) Pascal, file handling is much more easier than in Delphi: in Pascal we have Assign instead of AssignFile and Close instead of CloseFile . The syntax for these two procedures can be found in the help system.

Upvotes: 0

Related Questions