Read Data from a Textfile lazarus / 61-Beginners ---------------------------------------------------------------------- jcaser1948 · 2017-11-26 16:50 I try still to update my Motor calculation program I try to read the Motordate written in a Textfile PROCEDURE ReadFile; VAR   motorname:AnsiString;    Filevar: file; BEGIN Window(20,2,60,22);   REPEAT      motorname:='????????.MOT';       DirList(motorname);     schwach;     WRITELN;      WRITELN('NAME OF FILE TO BE LOADED ?(without extension) ');     stark;     READLN(Motorname);     UpCase(motorname);     Motorname := Motorname + '.MOT';     ok:=false;     WritelN(Motorname);     writeln;        assign(filevar,Motorname);         FileMode := fmOpenRead;  // open for read only            Rewrite (filevar);            RESET(filevar);      IF NOT FileExists(motorname) THEN                                        BEGIN                                        WRITELN('CAN NOT find file : ',Motorname);                                         WRITELN;                                        pausa;                                         WRITELN;                                          ok:=False;                                          END;                                   TRY                                  while not eof(filevar) do                                   BEGIN                                    ReadData;                                    ok:=true;                                    END;                                  except     writeln('File Error - Please Check Your File');                                end;     Close(filevar);       answer := 'N';      WRITELN(Motorname,' Do you want to load another Motor instead?(Y/N) ');       pausa;       answer:=Upcase(ReadKey);       If answer= 'Y' Then ok:= False Else ok:= True;   UNTIL ok=True;   WRITELN(Motorname,' loaded.');  END; And this is the Procedure ReadData  PROCEDURE ReadData;   BEGIN   READLN(filevar,kopf.dia);   READLN(filevar,kopf.mes);   READLN(filevar,kopf.anho);   READLN(filevar,kopf.motortyp);   READLN(filevar,kopf.Bearbeiter);   READLN(filevar,kopf.Statorblechnr);   READLN(filevar,kopf.Rotorblechnr);   READLN(filevar,kopf.Angebotsnummer);   READLN(filevar,kenrot);   READLN(filevar,schltg);   READLN(filevar,polzahl);   READLN(filevar,motfil);   READLN(filevar,pabkw);   READLN(filevar,uv);   READLN(filevar,freq);   READLN(filevar,cosphi);   READLN(filevar,etam);   READLN(filevar,delta);   READLN(filevar,N1);   READLN(filevar,n2);    {more Data}  CLOSE(filevar);  END;    The procedure does mot read anything at all    , bot instead write the Motor Data Textfiles  .MOT  with blanks Can ayione help me? Thanks in advance ---------------------------------------------------------------------- tudi_x · 2017-11-26 17:11 hi, for me the easiest way to read contents from a file is to use TStringList.LoadFromFile(). it works for me in 80% of cases. maybe it could get you started. ---------------------------------------------------------------------- Leledumbo · 2017-11-26 17:21 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  assign(filevar,Motorname);        FileMode := fmOpenRead;  // open for read only           Rewrite (filevar);           RESET(filevar); Where did you learn file handling? It's either Rewrite (for writing) or Reset (for reading), not both (and I don't know the behavior if you do). When you do the former, you erase the content of existing file. Moreover, System.FileMode should not be assigned with values from SysUtils.fm*, but System.fm*. Read: http://wiki.freepascal.org/File_Handling_In_Pascal https://www.freepascal.org/docs-html/rtl/system/index-2.html ---------------------------------------------------------------------- Handoko · 2017-11-26 17:54 @jcaser1948 I am not really sure what you want to do. But I ever wrote an example showing how to read and append data, maybe you can learn something from that code: http://forum.lazarus.freepascal.org/index.php/topic,37766.msg254800.ht ml#msg254800 ---------------------------------------------------------------------- molly · 2017-11-26 18:37 Quote from: jcaser1948 on November 26, 2017, 05:50:22 pmI try still to update my Motor calculation program The procedure does mot read anything at all    , bot instead write the Motor Data Textfiles  .MOT  with blanks Updating seems to suggest you have a working program (at least one that worked with what i assume is Turbo Pascal and/or Delphi) yet you keep on showing code that contains some sort of errors. In principle you should be able to compile your already existing code base. However there are some caveats as FPC has matured beyond TP/Delphi (old Delphi). That requires some minimal changes to your code in order to let it compile for Free Pascal. That is, unless you wish to update your code in the process. I would suggest to start with trying to compile your existing program's sources and handle each encountered error (when compiling that code with FPC) one at a time, e.g. do not try to rewrite your existing code unless it is related to fixing compilation errors. ---------------------------------------------------------------------- jamie · 2017-11-26 18:38 I guess it depends if this is a class project or actual  personal use. First off, as stated before, you do RESET for read and you don't play with the fileMode variable in this case.  You should also query the IoResults after each operation because if a failure occurs,  following Io operations will fail if the internal InOUtRes variable is not 0, which gets cleared when you use the IoResult Function.  Also, to make sure you don't have a left over IOresult still pending at the start of the operation, you should do InOutRes := 0 or just IoResult; to clear it. ---------------------------------------------------------------------- molly · 2017-11-26 19:03 Quote from: jamie on November 26, 2017, 07:38:56 pmI guess it depends if this is a class project or actual  personal use. True, but since TS wrote in the other thread.. Quote from: jcaser1948 on November 26, 2017, 05:39:39 pmThanks to everione.I try to update an old Motor calculations program I wrote about 25 years agoNow that I am retired I find the time and the muse to do this .BuT I have plente of problems with reading and write files on the Disk, something was was not so difficult in Turpopascal 3 ... I can only assume it is a personal project, at least not related to some teaching course. ---------------------------------------------------------------------- jcaser1948 · 2017-11-27 08:37 Thaks to everione. I will try to implement your suggestions in a small programm and see what it happens. I wrote this program in Torbopascal 3 about 22 years ego in order not to do the motor calculations by hand. Now I have time and try to update the code. The code for the rest of the prgramm works perfectly, only with the file handling routines a some big issues ---------------------------------------------------------------------- Thaddy · 2017-11-27 09:47 A 22 year old string type may be a shortstring...Not an Ansistring. A 22 year old piece of code may need a packed record reading from file and writing to file. Actually that is still best practice