Author Topic: 'ReadStr' ?  (Read 585 times)

J-G

  • Hero Member
  • *****
  • Posts: 1281
'ReadStr' ?
« on: August 11, 2026, 02:17:42 pm »
Looking back at some projects I did some tme ago, I've found a call to 'ReadStr' which the complier hinted that it didn't recognise - and I couldn't find a function or procedure that had that name  -  but the project did compile. A little research and I found that it actually calls function TControl.GetText in [Control.inc].

Today I would use :
Code: Pascal  [Select][+][-]
  1. Numb := StrToInt(Orig_Numb.Caption);

Whereas my old code has :
Code: Pascal  [Select][+][-]
  1. ReadStr(Orig_Num.Caption,Numb);

This discrepancy has me wondering why I used the ReadStr?  when today I can't remember ever knowing that such a call existed.  It may have been suggested to me via a forum post but I can't find such.

My real question is "Is there any benefit (or Dis) to either method cited"?
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12995
  • FPC developer.
Re: 'ReadStr' ?
« Reply #1 on: August 11, 2026, 02:55:08 pm »
read/writestr are read and writes to and from strings.  Some other Pascal dialects have them, and as it seemed useful, they were enabled in other modes too. I assume errorhandling is the same with {$I-}  and checking IORESULT.

Strtoint is the delphi way, and can throw exceptions, better use strtointdef or trystrtoint.

J-G

  • Hero Member
  • *****
  • Posts: 1281
Re: 'ReadStr' ?
« Reply #2 on: August 11, 2026, 03:07:16 pm »
Thanks @Marcov,  as is fairly usual, my questions elicit more!!

At least I am better informed over ReadStr but yet again I'll need to do more research on strtointdef and trystrtoint   ::)
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Thaddy

  • Hero Member
  • *****
  • Posts: 19805
  • Glad to be alive.
Re: 'ReadStr' ?
« Reply #3 on: August 12, 2026, 02:08:25 pm »
we also have str/val for int to string to int conversions and purists would call only those Pascal.
These are intrinsics. ReadStr/WriteStr come from ISO pascal.
« Last Edit: August 12, 2026, 02:10:06 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

J-G

  • Hero Member
  • *****
  • Posts: 1281
Re: 'ReadStr' ?
« Reply #4 on: August 12, 2026, 03:37:39 pm »
we also have str/val for int to string to int conversions and purists would call only those Pascal.
These are intrinsics. ReadStr/WriteStr come from ISO pascal.
I'll have to admit that I'd forgotten about Str / Val but (having looked again at how they 'work' ) feel that IntToStr/StrToInt are simpler options.

With Str(X,S) the S must be a variable whereas I generally wish to assign the value of a number to the 'Caption' of a TLabel (or suchlike). No doubt I could create a var which I thereafter assign to the 'Caption' but that is just counter-productive.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

PascalDragon

  • Hero Member
  • *****
  • Posts: 6430
  • Compiler Developer
Re: 'ReadStr' ?
« Reply #5 on: August 14, 2026, 08:33:34 pm »
read/writestr are read and writes to and from strings.  Some other Pascal dialects have them, and as it seemed useful, they were enabled in other modes too. I assume errorhandling is the same with {$I-}  and checking IORESULT.

Strtoint is the delphi way, and can throw exceptions, better use strtointdef or trystrtoint.

And in {$I+} it will just as well throw exceptions:

Code: Pascal  [Select][+][-]
  1. program treadstr;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils;
  7.  
  8. var
  9.   s: String;
  10.   l: LongInt;
  11. begin
  12.   try
  13.     s := 'Foo';
  14.     ReadStr(s, l);
  15.     Writeln(l);
  16.   except
  17.     on e: Exception do
  18.       Writeln(e.ClassName, ': ', e.Message);
  19.   end;
  20. end.

Output:

Code: [Select]
EInOutError: Invalid input

 

TinyPortal © 2005-2018