Recent

Author Topic: Readkey problem  (Read 9425 times)

DesJardins

  • New Member
  • *
  • Posts: 26
Readkey problem
« on: May 18, 2018, 07:48:29 pm »
I am in the process of converting a Turbo 4 program to Free Pascal.  The old program uses ch:=readkey to pause and proceed.  That for some reason is not working for FP.  Example:

procedure Msg(S: AnyString);
begin
  GotoXY(1,24);  ClrEol;
  Write(S);
 end;   

lots of code..........

  if gpmsf < 2.5 then
  begin
    msg('LIGHT WATER LOADING.  ADJUST FOR POOR DISTRIBUTION, PROGRAM CONTINUES');
     ch:=readkey;
  end; 

But when I press any key or enter the FP program does not continue.   the program freezes.  It happens lots of places in the program.

Any idea why this would occur?

Richard


 

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Readkey problem
« Reply #1 on: May 18, 2018, 08:17:32 pm »
Hello DesJardins,
Welcome to the forum.

Bugs can hide in anywhere inside the code. Without inspecting the whole source code, not much we can say.

Please compress the source code and send the zip file to the forum.

jamie

  • Hero Member
  • *****
  • Posts: 6131
Re: Readkey problem
« Reply #2 on: May 18, 2018, 11:09:31 pm »
Back in the DOS days, that is not how I did it.., not saying it won't work here but....


While Not KeyPressed do;
ch :=ReadKey; // to flush the buffer..


---
 Or just use "ReadLn"

The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Readkey problem
« Reply #3 on: May 18, 2018, 11:31:21 pm »
While Not KeyPressed do;
ch :=ReadKey; // to flush the buffer..

The first line seems superflous to me, readkey will simply not return until a key is pressed.
Clearing the keyboard buffer should be done like

Code: Pascal  [Select][+][-]
  1.   while KeyPressed do ReadKey;

This is to ensure that when a user presses e.g. Up, ReadKey wil return #0, and the remaining part of the KeyCode will be in the second ReadKey.

So my Promt() function would do something like:

Code: Pascal  [Select][+][-]
  1.   while KeyPressed do ReadKey; //clear keyboardbuffer first (I factored that out to a ClrKbd() procedure)
  2.   write(Msg);
  3.   repeat
  4.     Ch := ReadKey;
  5.     if (Ch = #0) then ReadKey; //dismiss function keys and arrow keys
  6.   until (ch in AllowedKeys); //A set of Char, one of the parameters of the function

Bart

DesJardins

  • New Member
  • *
  • Posts: 26
Re: Readkey problem
« Reply #4 on: May 29, 2018, 07:13:04 am »
Okay, I finally had time to try your suggestions, and still it does not work.  All I want to do is pause so they can read “ msg”  and then continue when “any key” is pressed.
 
I really think I might have a basic flaw in the FP program.  However,  I wrote a simple program and ch:-readkey works just fine.  My old pascal programs all worked fine, and anther new program I wrote in FP works fine.  Why not here?  When I used readln in this part of the code I got and error(103) which I think relates to "file not open"??????  Neither one works at this place in the code.  I copied the code to Notes, erased the sections in the program and then copied the Notes text back to the program.  It did not solve the problem.  Where does Readkey reside, in CRT?, Sysutils ?  Keyboard?  Could I have a flaw in my Lazarus/FP download? 

When I open to run this program ch:=readkey works to get me started, so why does it not work later in the program?

I have another problem in the program, but I think I need to solve this one first, because it may be related.  This is because I can see the program runs, and I does give the right answers to the screen, but it freezes and the arrow keys will not continue to work and I cannot simply change some input data and  continue to run again.  If I use readln at the end of a “msg” it totally closes the program.

  I admit that I do not understand  your  reference to flush a buffer..  What buffer?  where?

To see what I am doing look at my webpage www.rjdesjardins.com and see the cooling tower program at the bottom that uses a very simple spreadsheet for the input /output screen, takes data from the screen, makes calculations and puts the results back on the screen.  If you do try to use inputs outside my design limits messages (msg) come up as the program runs.  It is designed to keep running, and seldom causes a total “abort”, and even then it will start over without you having to totally restart the program.

DesJardins

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Readkey problem
« Reply #5 on: May 29, 2018, 07:36:54 am »
are you using lazarus? if yes, is it on windows? if yes, is the win32 gui option in the "project options\compiler options\config and target" set? unset it. if any of the above is no then I can't help.
« Last Edit: May 29, 2018, 07:39:15 am by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Readkey problem
« Reply #6 on: May 29, 2018, 01:03:11 pm »
OK, at this point, without having the sourcecode we cannot help you.

From the website you mentioned, I guess this is a Windows program.
If so, in the main program file add this (after the program declaration and above the uses section):
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}

This ensures you can write to stdin and stout (input/output handles), which by default are "closed" in Windows (the compiler assumes you build a Windows GUI by default).

If you cannot fix the "issue with readkey" there are not many options left:
  • Use TurboPascal to build the executable and convince your end-users to run the application in DosBox (current Windows cannot run 16-bit applications).
  • Redesign you progam to be a native Windows GUI.

In the long run option 2 will be the better one.
However this required the end-users being able to run Windows, whivh may or may not be a problem.

Bart

Nitorami

  • Hero Member
  • *****
  • Posts: 501
Re: Readkey problem
« Reply #7 on: May 29, 2018, 02:27:40 pm »
readkey is in the crt unit. You might want to try the equivalent from the more modern keyboard unit, which is a bit more complicated to handle, but it probably does not support the old gotoxy and similar console features (which are also part of crt).

To me your problem sounde like you made an illegal I/O access somewhere else in the program, writing to a file not open, forgot to close, or similar. The operating system keeps that in mind and then your next I/O access may produce odd results, unless you explicitly check the OS's error code. Try to compile the program with I/O checking ON {$I+} and see whether it throws an error before it even arrives at your messagebox.

DesJardins

  • New Member
  • *
  • Posts: 26
Re: Readkey problem
« Reply #8 on: July 08, 2018, 02:41:45 am »
Readkey is NOT working after file is open for reading (assign / reset ) and it does NOT on work after file is closed. Why would opening and closing a file cause Readkey or Readln to not work?

A portion of the program is shown below: 
The program uses a simple spreadsheet (a la old VisaCalc) as input/output screen.  See www.rjdesjardins.com for a picture of screen.
User makes changes to screen (some calculations made on the screen with formulas).  When the program runs it takes data from the screen, runs the calculations and puts results back on the screen.
 
When called to RUN  (or when called as a HELP screen) it reads a text file and stores the data as properties.  When cell B6 is called for HELP it lists all the items, and when Running it gets the properties for the selected item listed in cell B6 and uses the data to make calculations.

Readkey is used several times prior to opening and reading the text file when warning messages are required, and it DOES work to stop the program flow to allow the message to be read and proceeds when any key is pressed.  Readkey works up until the text file is opened, and it never goes back to working after the text file is closed.  Readkey also will not work when tested within the open and close of the read file, but I don’t need to stop it when reading – I just want it to work afterwards so the program will keep running – it freezes the program the next time Readkey is required.  (Readln also does not work).

When the program is first started it opens a file and populates the spreadsheet cells.  When the start file closes Readkey works fine.  I can also Save and Load a screen and ReadKey works afterwards. But when I read the text file Readkey stops working.
Why would opening and/or closing a read file cause it to fail?

EXAPLE:


{file chelp.pas;}  {LAST UPDATE 1/04/93}
Unit CF20help;
{$mode objfpc}{$H+}
INTERFACE

USES CRT,DOS, cf20Var, CF20windo, sysutils ;

procedure GetFillData;
procedure Help;

IMPLEMENTATION
uses CF20_2;

Procedure GetFillData; { This gets input from file of fill data  and puts it in an array}
var
  S: AnyString;
    {FillName = FillList = Array [0..Maxx, BARR..nos] of Real; declared in CF20Var }
    {Fd = FillList = Array [0..Maxx, BARR..nos] of Real declared in CF20Var;}
begin
                 { Test for Readkey DID work here }
  S:='CFlowFil.CFT';     { this is a text file of data in the same folder}
     (*  Test to see if Readkey works here
           gotoXY(75,25);   {5 lines to test if Readkey DID work here}
          write('CHelp 22');
          ch:=readkey;
          write(ch);     { Readkey did work here with any letter or <return>}
          ch:=readkey;   { Readkey did work here as the program continued }
       *)
    Assign (input,'CFlowFil.CFT');    {open CFlowFil.CFT}
  try
    Reset(input);
    i:=-1;
    numbob:=0;
    While not eof do
     begin
        i:=i+1;
        numbob:=numbob+1;
        Readln(FillName[i,Filtype]);
        Readln(FillName[i,TestSource]);
        Readln(FillName[i,Mfg]);
        Readln(FillName[i,Spacing]);
        Readln(FillName[i,nozzle]);
        Readln(Fd[i,BARR], Fd[i,ARFFF],Fd[i,PP1],Fd[i,PP2],Fd[i,PP3],Fd[i,PP4],Fd[i,PP5],Fd[i,PP6]);
        Readln(Fd[i,ACTI],Fd[i,mCTI],Fd[i,Aos],Fd[i,mos],Fd[i,nCTI],Fd[i, nos]);
     end;     {I  know the data file CFlowFil.CFT was read because I can print to screen later}
   Close(input);    {close CFlowFil.CFT}
    Except
          gotoXY(75,25);
          write('Error closing file');  { This did show on screen}
       (*   ch:=readkey;
          write(ch);      {Readkey did NOT work here}
          ch:=readkey;   {Readkey did NOT work here}
         *)
end;

end;
________________________________
Procedure help;
var
  OrigMode:Integer;
  count:Integer;
begin
   count:=0;
   OrigMode:=LastMode;
 {  Initwindow;
   Makewindow(5,1,73,24,4,15);}
   clrscr;


if (FX='B') and (FY=6) then                   {Cell B6 is where the Fill Designation is on the spreadsheet}
begin
   Writeln('   Fill        Test    Manufacturer  Sheet   Nozzle');
   Writeln('   Type       Source                Spacing   Type');
   writeln;
   GetFillData;              {This is the call to Procedure above}
     (*     gotoXY(75,25);
          write('CHelp 162');   {This prints on the screen}
          readln;         {if readln is used the program just quits}
          ch:=readkey;
        write(ch);     { Readkey did NOT write  ch to screen}
          ch:=readkey;    { Readkey did NOT work here}
      *)

   i:=-1;
   count:=0;
   while (i<numbob-1) do
    begin
     i:=i+1;
     count:=count+1;
     WriteLN(' '+
      FillName[i,Filtype]:10,FillName[i,TestSource]:13,
      FillName[i,Mfg]:11, FillName[i,spacing]:9,
      FillName[i,nozzle]:12);
      if count>15 then            {the first 15 does print to screen}
     begin
        WRITELN;
        TEXTCOLOR(YELLOW);
        writeln(' PRESS ANY KEY FOR MORE FILL TYPES');
        tEXTCOLOR(WHITE);
        count:=0;
      ch:=readkey;          {program stops here because of readkey}
        clrscr;
        Writeln('   Fill        Test    Manufacturer  Sheet   Nozzle');
        Writeln('   Type       Source                Spacing   Type');
        writeln;
     end;
   end;
  end else

(NOW, PLEASE TELL ME WHY OPENING A FILE “ASSIGN” WOULD NAGATE READEKY  ??????????)
In my original post I wrote “  When I used readln in this part of the code I got and error(103) which I think relates to "file not open"?????? I have not gotten that error(103) lately, but maybe it is a clue to what is happening.  But, CflowFil.CFT does open, the data is read, and it does close. The original file will run after the read file closes, but it freezes at the first ReadKey.

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1228
Re: Readkey problem
« Reply #9 on: July 08, 2018, 04:26:36 am »
hello,
maybe there is something wrong with stdin when you use ReadKey  after you have opened a file.
You can try to use a Getkeyevent in place of readkey like this for example :
Code: Pascal  [Select][+][-]
  1. program ReadkeyTest;
  2. uses crt,keyboard;
  3. var ch : Char;
  4.     myLine : String;
  5.     Key: TKeyEvent;
  6. begin
  7.   InitKeyboard;
  8.   writeln('Readkey Test');
  9.   writeln('Press a key to continue !');
  10.   //ch := Readkey;
  11.   Key:= GetKeyEvent;
  12.   writeln('open the ReadKeyTest.pas file');
  13.   AssignFile(input,'ReadKeyTest.pas');    {open ReadKeyTest.pas}
  14.   try
  15.     Reset(input);
  16.     Read(myLine);
  17.     writeln(myLine);
  18.     writeln('Press a key to continue !');
  19.     //ch := Readkey;
  20.     Key:= GetKeyEvent;
  21. //  Key:= TranslateKeyEvent(Key);
  22. //  ch := GetKeyEventChar(Key);
  23.     CloseFile(input);    {close ReadKeyTest.pas}
  24.     writeln('File closed');
  25.     writeln('Press a key to continue !');
  26.     //ch := Readkey;
  27.     Key:= GetKeyEvent;
  28.     Except
  29.           writeln('Error closing file');  { This did show on screen}
  30.           writeln('Press a key to continue !');
  31.          //ch := Readkey;
  32.          Key:= GetKeyEvent;
  33.   end;
  34. end.

this program doesn't work if i put Readkey in place of GetKeyEvent.

Another soiution is to use FileOpen, FileRead, FileClose Functions to handle files.
You can also read the content of the file in a stringList and then read each line :
Code: Pascal  [Select][+][-]
  1. //uses classes;
  2. var I:integer
  3. var TheLines: TstringList
  4. begin
  5.   TheLines := TStringList.Create;  
  6.   TheLines.LoadFromFile('ReadKeyTest.pas');
  7.   for I:=0 to TheLines.Count -1 do
  8.   begin
  9.        writeln(TheLines[I]);
  10.   end;
  11.   TheLines.Free;    
  12. end;
  13.  

Friendly, J.P
« Last Edit: July 08, 2018, 05:57:27 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Readkey problem
« Reply #10 on: July 08, 2018, 06:29:05 am »
Readkey is NOT working after file is open for reading (assign / reset ) and it does NOT on work after file is closed. Why would opening and closing a file cause Readkey or Readln to not work?

A portion of the program is shown below: 
The program uses a simple spreadsheet (a la old VisaCalc) as input/output screen.  See www.rjdesjardins.com for a picture of screen.
User makes changes to screen (some calculations made on the screen with formulas).  When the program runs it takes data from the screen, runs the calculations and puts results back on the screen.
 
When called to RUN  (or when called as a HELP screen) it reads a text file and stores the data as properties.  When cell B6 is called for HELP it lists all the items, and when Running it gets the properties for the selected item listed in cell B6 and uses the data to make calculations.

Readkey is used several times prior to opening and reading the text file when warning messages are required, and it DOES work to stop the program flow to allow the message to be read and proceeds when any key is pressed.  Readkey works up until the text file is opened, and it never goes back to working after the text file is closed.  Readkey also will not work when tested within the open and close of the read file, but I don’t need to stop it when reading – I just want it to work afterwards so the program will keep running – it freezes the program the next time Readkey is required.  (Readln also does not work).

When the program is first started it opens a file and populates the spreadsheet cells.  When the start file closes Readkey works fine.  I can also Save and Load a screen and ReadKey works afterwards. But when I read the text file Readkey stops working.
Why would opening and/or closing a read file cause it to fail?

EXAPLE:


{file chelp.pas;}  {LAST UPDATE 1/04/93}
Unit CF20help;
{$mode objfpc}{$H+}
INTERFACE

USES CRT,DOS, cf20Var, CF20windo, sysutils ;

procedure GetFillData;
procedure Help;

IMPLEMENTATION
uses CF20_2;

Procedure GetFillData; { This gets input from file of fill data  and puts it in an array}
var
  S: AnyString;
    {FillName = FillList = Array [0..Maxx, BARR..nos] of Real; declared in CF20Var }
    {Fd = FillList = Array [0..Maxx, BARR..nos] of Real declared in CF20Var;}
begin
                 { Test for Readkey DID work here }
  S:='CFlowFil.CFT';     { this is a text file of data in the same folder}
     (*  Test to see if Readkey works here
           gotoXY(75,25);   {5 lines to test if Readkey DID work here}
          write('CHelp 22');
          ch:=readkey;
          write(ch);     { Readkey did work here with any letter or <return>}
          ch:=readkey;   { Readkey did work here as the program continued }
       *)
    Assign (input,'CFlowFil.CFT');    {open CFlowFil.CFT}
  try
    Reset(input);
    i:=-1;
    numbob:=0;
    While not eof do
     begin
        i:=i+1;
        numbob:=numbob+1;
        Readln(FillName[i,Filtype]);
        Readln(FillName[i,TestSource]);
        Readln(FillName[i,Mfg]);
        Readln(FillName[i,Spacing]);
        Readln(FillName[i,nozzle]);
        Readln(Fd[i,BARR], Fd[i,ARFFF],Fd[i,PP1],Fd[i,PP2],Fd[i,PP3],Fd[i,PP4],Fd[i,PP5],Fd[i,PP6]);
        Readln(Fd[i,ACTI],Fd[i,mCTI],Fd[i,Aos],Fd[i,mos],Fd[i,nCTI],Fd[i, nos]);
     end;     {I  know the data file CFlowFil.CFT was read because I can print to screen later}
   Close(input);    {close CFlowFil.CFT}
    Except
          gotoXY(75,25);
          write('Error closing file');  { This did show on screen}
       (*   ch:=readkey;
          write(ch);      {Readkey did NOT work here}
          ch:=readkey;   {Readkey did NOT work here}
         *)
end;

end;
________________________________
Procedure help;
var
  OrigMode:Integer;
  count:Integer;
begin
   count:=0;
   OrigMode:=LastMode;
 {  Initwindow;
   Makewindow(5,1,73,24,4,15);}
   clrscr;


if (FX='B') and (FY=6) then                   {Cell B6 is where the Fill Designation is on the spreadsheet}
begin
   Writeln('   Fill        Test    Manufacturer  Sheet   Nozzle');
   Writeln('   Type       Source                Spacing   Type');
   writeln;
   GetFillData;              {This is the call to Procedure above}
     (*     gotoXY(75,25);
          write('CHelp 162');   {This prints on the screen}
          readln;         {if readln is used the program just quits}
          ch:=readkey;
        write(ch);     { Readkey did NOT write  ch to screen}
          ch:=readkey;    { Readkey did NOT work here}
      *)

   i:=-1;
   count:=0;
   while (i<numbob-1) do
    begin
     i:=i+1;
     count:=count+1;
     WriteLN(' '+
      FillName[i,Filtype]:10,FillName[i,TestSource]:13,
      FillName[i,Mfg]:11, FillName[i,spacing]:9,
      FillName[i,nozzle]:12);
      if count>15 then            {the first 15 does print to screen}
     begin
        WRITELN;
        TEXTCOLOR(YELLOW);
        writeln(' PRESS ANY KEY FOR MORE FILL TYPES');
        tEXTCOLOR(WHITE);
        count:=0;
      ch:=readkey;          {program stops here because of readkey}
        clrscr;
        Writeln('   Fill        Test    Manufacturer  Sheet   Nozzle');
        Writeln('   Type       Source                Spacing   Type');
        writeln;
     end;
   end;
  end else

(NOW, PLEASE TELL ME WHY OPENING A FILE “ASSIGN” WOULD NAGATE READEKY  ??????????)
In my original post I wrote “  When I used readln in this part of the code I got and error(103) which I think relates to "file not open"?????? I have not gotten that error(103) lately, but maybe it is a clue to what is happening.  But, CflowFil.CFT does open, the data is read, and it does close. The original file will run after the read file closes, but it freezes at the first ReadKey.
Code: Pascal  [Select][+][-]
  1. USES CRT,DOS, cf20Var, CF20windo, sysutils ;
  2.  
  3. procedure GetFillData;
  4. procedure Help;
  5.  
  6. IMPLEMENTATION
  7. uses CF20_2;
  8.  
  9. Procedure GetFillData; { This gets input from file of fill data  and puts it in an array}
  10. var
  11.   S: AnyString;
  12.     {FillName = FillList = Array [0..Maxx, BARR..nos] of Real; declared in CF20Var }
  13.     {Fd = FillList = Array [0..Maxx, BARR..nos] of Real declared in CF20Var;}
  14. begin
  15.                  { Test for Readkey DID work here }
  16.   S:='CFlowFil.CFT';     { this is a text file of data in the same folder}
  17.      (*  Test to see if Readkey works here
  18.            gotoXY(75,25);   {5 lines to test if Readkey DID work here}
  19.           write('CHelp 22');
  20.           ch:=readkey;
  21.           write(ch);     { Readkey did work here with any letter or <return>}
  22.           ch:=readkey;   { Readkey did work here as the program continued }
  23.        *)
  24.     Assign (input,'CFlowFil.CFT');    {open CFlowFil.CFT}
  25.   try
  26.     Reset(input);
  27.     i:=-1;
  28.     numbob:=0;
  29.     While not eof do
  30.      begin
  31.         i:=i+1;
  32.         numbob:=numbob+1;
  33.         Readln(FillName[i,Filtype]);
  34.         Readln(FillName[i,TestSource]);
  35.         Readln(FillName[i,Mfg]);
  36.         Readln(FillName[i,Spacing]);
  37.         Readln(FillName[i,nozzle]);
  38.         Readln(Fd[i,BARR], Fd[i,ARFFF],Fd[i,PP1],Fd[i,PP2],Fd[i,PP3],Fd[i,PP4],Fd[i,PP5],Fd[i,PP6]);
  39.         Readln(Fd[i,ACTI],Fd[i,mCTI],Fd[i,Aos],Fd[i,mos],Fd[i,nCTI],Fd[i, nos]);
  40.      end;     {I  know the data file CFlowFil.CFT was read because I can print to screen later}
  41.    Close(input);    {close CFlowFil.CFT}
  42.     Except
  43.           gotoXY(75,25);
  44.           write('Error closing file');  { This did show on screen}
  45.        (*   ch:=readkey;
  46.           write(ch);      {Readkey did NOT work here}
  47.           ch:=readkey;   {Readkey did NOT work here}
  48.          *)
  49. end;
  50.  
  51. end;
  52. ________________________________
  53. Procedure help;
  54. var
  55.   OrigMode:Integer;
  56.   count:Integer;
  57. begin
  58.    count:=0;
  59.    OrigMode:=LastMode;
  60.  {  Initwindow;
  61.    Makewindow(5,1,73,24,4,15);}
  62.    clrscr;
  63.  
  64.  
  65. if (FX='B') and (FY=6) then                   {Cell B6 is where the Fill Designation is on the spreadsheet}
  66. begin
  67.    Writeln('   Fill        Test    Manufacturer  Sheet   Nozzle');
  68.    Writeln('   Type       Source                Spacing   Type');
  69.    writeln;
  70.    GetFillData;              {This is the call to Procedure above}
  71.      (*     gotoXY(75,25);
  72.           write('CHelp 162');   {This prints on the screen}
  73.           readln;         {if readln is used the program just quits}
  74.           ch:=readkey;
  75.         write(ch);     { Readkey did NOT write  ch to screen}
  76.           ch:=readkey;    { Readkey did NOT work here}
  77.       *)
  78.  
  79.    i:=-1;
  80.    count:=0;
  81.    while (i<numbob-1) do
  82.     begin
  83.      i:=i+1;
  84.      count:=count+1;
  85.      WriteLN(' '+
  86.       FillName[i,Filtype]:10,FillName[i,TestSource]:13,
  87.       FillName[i,Mfg]:11, FillName[i,spacing]:9,
  88.       FillName[i,nozzle]:12);
  89.       if count>15 then            {the first 15 does print to screen}
  90.      begin
  91.         WRITELN;
  92.         TEXTCOLOR(YELLOW);
  93.         writeln(' PRESS ANY KEY FOR MORE FILL TYPES');
  94.         tEXTCOLOR(WHITE);
  95.         count:=0;
  96.       ch:=readkey;          {program stops here because of readkey}
  97.         clrscr;
  98.         Writeln('   Fill        Test    Manufacturer  Sheet   Nozzle');
  99.         Writeln('   Type       Source                Spacing   Type');
  100.         writeln;
  101.      end;
  102.    end;
  103.   end else
Really? You assign a text file to the input and you are asking why readkey does not work any more? Well to put it simple, declare your own variables to use and stop overriding system variables. Input is mend to be  attached to the keyboard not a text file.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

DesJardins

  • New Member
  • *
  • Posts: 26
Re: Readkey problem
« Reply #11 on: July 16, 2018, 05:40:55 pm »
When the main program starts it loads a simple spreadsheet,
then it populates the spreasheet by reading a file as follows:

 FileName:='CFX.CFT';                       {From cf20}
 Assign (RDFile, FileName);
 Reset(RDFile);
   for FX:='A' to FXmax do
     for FY:=1 to FYmax do read (RDFile, Sheet[FX,Fy]);
   FX:='B'; FY:= 1;
 Close(RDFile);

That part works. 

And I did what you suggested and got rid of "input" as a variable.
This seems to solve the problem of Ch:=Readkey and Readln not working.
But now I have aonther problem that I just do not understand.  I cannot read the text
file that has a lot of data I want to use for calculatoins.  When I put "input" back in
it does work to read the file and write to the Help screen, or to run the program, but
the program stops because of using "input" as the variable.  I tried to see where "input"
was a reserved word, but could not find it.  Where ever it is I must be closing a file
when I use "input" so Readkey won't work.

 A small sample of the text file is as follows: (2 of 30 sets of data shown)
  Text file is "CflowFil.cft"

MF19         {this is the name of 1st type of Fill}
KERSON
MUNT
19 MM
MUNT
40 0.17 0.05962 0.022006 -0.01032 1.795 0.023466  0.4
0.7514  0.6401  1.0996  0.6324  0.6463  0.6393
ATPF           {this is the name of 2nd type of fill}
FULK       
ATP
1.63 IN
MAR
40 -0.17 0.075753 0.02358 -0.01202 1.931 0.024658 0.4
0.8467 0.6264 1.2381 0.6176 0.7989 0.7913

There is a master varialbe Unit (CF20Var) that includes the following:

 Type                    {FROM CF20VAR}

 FilData = (Filtype, TestSource, Mfg, spacing, nozzle);
 FillData = (BARR, ARFFF, PP1, PP2, PP3, PP4, PP5, PP6, ACTI, mCTI, Aos, mos, nCTI, nos);
 Fill = Array [0..Maxx,Filtype..nozzle] of string[10];
 FillList = Array [0..Maxx, BARR..nos] of Real;

 Var                       {FROM CF20VAR}
    FillName:Fill;
    Fd:FillList;
 

SOMEHOW I THINK MY PROBLEM AS TO DO WITH "TYPES" AND "VARIABLES", and maybe RECORDS.
 I CANNOT GET IT TO READ THE TEXT FILE INTO MY VARIALBLEs "FillName" and "Fd".
There are 2 Units that use this, one is the main calculations, and the other
is a "Help" screen from which you can choose the fill you want to use.
This is the code for the "help" screen that is NOT reading the text file in Procedure
 "GetFillDAta". so it is not getting printed in Procedure "Help."
It all worked with "input" in old Turbo 4.  Sorry for confussion of "Fil, Fill, FillName,
FileName, as I know I should rename some of them to make it easier to read.

{file chelp.pas;}  {LAST UPDATE 1/04/93}
Unit CF20help;
{$mode objfpc}{$H+}

INTERFACE
USES CRT,DOS, cf20Var, CF20windo, sysutils, CF20_2 ;

procedure GetFillData;
procedure Help; 

IMPLEMENTATION

Procedure GetFillData; { This gets input from file of fill data
                          and puts it in an array}
var
  FDat: textfile;   {used to be "input" without a type and was not listed as a Var}

begin
    if NOT fileexists('cflowFil.CFT') then begin
    writeln('ERROR: File CFlowFil.CFT not found. It must be in same folder as Program');
    writeln('Program Halted.  Move File CFlowFil.CFT to same folder and start over');
    readln;
    Halt;
end;

    Assignfile(FDat,'CFlowFil.CFT');    {open CFlowFil.CFT}
    Reset(FDat);
      i:=-1;
      numbob:=0;
      While not eof(Fdat) do
      begin
        i:=i+1;
        numbob:=numbob+1;
        Readln(FillName[i,Filtype]);
        Readln(FillName[i,TestSource]);
        Readln(FillName[i,Mfg]);
        Readln(FillName[i,Spacing]);
        Readln(FillName[i,nozzle]);
        Readln(Fd[i,BARR], Fd[i,ARFFF],Fd[i,PP1],Fd[i,PP2],Fd[i,PP3],Fd[i,PP4],Fd[i,PP5],Fd[i,PP6]);
        Readln(Fd[i,ACTI],Fd[i,mCTI],Fd[i,Aos],Fd[i,mos],Fd[i,nCTI],Fd[i, nos]);
      end;
   Close(FDat);    {close CFlowFil.CFT}
  end;

Procedure help;
var
  OrigMode:Integer;
  count:Integer;

begin
   count:=0;
   OrigMode:=LastMode;
 {  Initwindow;
   Makewindow(5,1,73,24,4,15);}
   clrscr;
 (*-------------*)
if (FX='B') and (FY=6) then   {this is cell address of grid}
begin
   Writeln('   Fill        Test    Manufacturer  Sheet   Nozzle');
   Writeln('   Type       Source                Spacing   Type');
   writeln;
   GetFillData;              {This is the call to Procedure GetFillData above}
   i:=-1;
   count:=0;
   while (i<numbob-1) do
    begin
     i:=i+1;
     count:=count+1;
     WriteLN(' '+
      FillName[i,Filtype]:10,FillName[i,TestSource]:13,
      FillName[i,Mfg]:11, FillName[i,spacing]:9,
      FillName[i,nozzle]:12);

      if count>15 then    {I cut out this section for this inquiry}
     begin
        {this section omitted because I only included 2 fills in text file example}
     end;
   end;
  end else   ..........................


THIS ALL WORKED IN TURBO4 (USING "INPUT" AS PREVIOUSLY TALKED ABOUT --SEE PREVIOUS
POSTINGS RE: READKEY;)
In the Help screen I only have to print Filtype, TestSource, Mfg, spacing, nozzle,
and I don't have to fool with "Fd", but I expect I will have the same problem reading
that data, too, when I run the program with the "RUN" procedure.

ALL I THINK I DID WAS CHANGE varialbe "INPUT" TO "Fdat" and made Fdat a Type "TextFile".
Is there a problem with TextFile as a Type and is it tied to other Types and variables.
Am I somehow Reading one file and writing another?  It is reading the text file because
write simple code and get it to print out inside the FetFillData procedure.
It is just not getting transferred to the Help procedure like it did inthe old program
when I used "input" as the varialble. All units use CF20Var. 

I have spent many days trying to figure this out..
   Why?   becuse I am obsessed!!!!!!!!!!

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: Readkey problem
« Reply #12 on: July 16, 2018, 05:51:51 pm »
Readkey simply does not work, if the file input is assigned to another file. It is bound to the CRT.

You can switch back to standard mode if you reassign input with an empty string

Code: Pascal  [Select][+][-]
  1. uses
  2.   crt;
  3. var
  4.   a: char;
  5.   s: string;
  6. begin
  7.   a := readkey;  //works
  8.   assign(input,'CFlowFil.CFT');
  9.   reset(input);
  10.   //after this readkey would not work
  11.   readln(s);
  12.   writeln(s);
  13.   close(input);
  14.   assign(input,'');
  15.   reset(input);
  16.   a:=readkey; //works again
  17. end.
  18.  
« Last Edit: July 16, 2018, 06:01:27 pm by Gammatester »

DesJardins

  • New Member
  • *
  • Posts: 26
Re: Readkey problem
« Reply #13 on: July 17, 2018, 12:37:54 am »
Thanks Gammastester!!!!!!!!!!!!

I have been looking all over for that, and I could not find anyplace that associated "input" to CRT or Keyboard.   I knew I had to be closing a file someplace, but I did not know how to open it.  How did you know to just leave a blank file open?  I really don't need to know, just curious.

Maybe I can get this working.  Now I only have about 8,000 lines code to check and re-do.

Gammatester

  • Jr. Member
  • **
  • Posts: 69
Re: Readkey problem
« Reply #14 on: July 17, 2018, 09:12:52 am »
I have been looking all over for that, and I could not find anyplace that associated "input" to CRT or Keyboard.   I knew I had to be closing a file someplace, but I did not know how to open it.  How did you know to just leave a blank file open?  I really don't need to know, just curious.
This well-known for at least 25 years. Here a relevant quote from the Turbo Pascal 7 Language Guide from 1992

Quote
To use the Crt unit, include it in your program's uses clause as
you would any other unit:

uses Crt;

The initialization code of the Crt unit assigns the Input and Output
standard text files to refer to the CRT instead of to DOS's standard
input and output files. These statements execute at the beginning
of a program:

AssignCrt(Input); Reset (Input) ;
AssignCrt(Output); Rewrite (Output) ;

This means that I/O redirection of the Input and Output files is no
longer possible unless these files are explicitly assigned back to
standard input and output by executing this:

Assign(Input, "); Reset (Input) ;
Assign (Output, "); Rewrite (Output) ;

A problem with newer documentation is, that they do not really focus
on the basic language and units, and often they are generated from
badly commented unit source.

See also https://www.freepascal.org/docs-html/current/rtl/crt/assigncrt.html
« Last Edit: July 17, 2018, 09:39:40 am by Gammatester »

 

TinyPortal © 2005-2018