2

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.
Andriy M
  • 76,112
  • 17
  • 94
  • 154
Sam Collins
  • 443
  • 5
  • 13
  • You're asking for help making this code work with FPC, not with Delphi, so I've adjusted your tags. Please beware that there are some differences between how different Pascal implementations behave. –  Oct 24 '14 at 18:23
  • Thanks for that I fixed it with {$mode objfpc } but now my problem is it exits with error code 217 – Sam Collins Oct 24 '14 at 20:11
  • the instruction I learnt to use in pascal FPC to close files is just `close(gameFile)` and also for assign it is `assign(gameFile)` Can't See the complete info about "error code 217" to give you a more detailed solution – sir psycho sexy Oct 25 '14 at 00:10
  • I seem to have fixed it all for now, I tried it a completely different way – Sam Collins Oct 25 '14 at 00:16
  • Why did you find it necessary to not only post a bunch of irrelevant code, but to then post all of the same code yet again? When asking questions here, include **only the code that is relevant to your question** so we don't have to sift through all of the extraneous noise to figure out what you're asking. See [How to create a Minimal, Complete Verifiable Answer](http://stackoverflow.com/help/mcve) for some tips on how to do so. – Ken White Oct 26 '14 at 01:58
  • I didn't mean to have the code twice, I don't know why it duplicated – Sam Collins Oct 26 '14 at 11:37
  • @KenWhite: I think Sam somehow managed to paste the entire question twice when editing. The duplicate part is now removed, but Sam, it would be nice if you tried and removed irrelevant bits out of what's left. – Andriy M Nov 07 '14 at 08:50

2 Answers2

8

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

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
0

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.

Bogdan Doicin
  • 2,342
  • 5
  • 25
  • 34
  • As Marco indicated, you can use TP style file handling with .CloseFile/.AssignFile if you set mode ObjFPC or Delphi... It's still old-style file handling. – reiniero Nov 14 '14 at 13:50