Recent

Author Topic: trouble with format  (Read 2238 times)

acp693

  • Jr. Member
  • **
  • Posts: 73
trouble with format
« on: March 07, 2013, 02:46:47 pm »
Hi, I'm trying to use the format function to write integers and floats to a file, With the following code, I get the following compiler error:

unit1.pas(590,8) Error: Incompatible types: got "SmallInt" expected "AnsiString"

Any idea what I'm doing wrong, I lifted the code more or less directly from this reference:

http://www.freepascal.org/docs-html/rtl/sysutils/format.html

Thank you

Albert


Code: [Select]
Var
resfile:textfile;
 s1,s2,fmt:string;   
begin
AssignFile(resFile, 'spec.ini');
Rewrite(resFile);

Fmt:='[%10d]';
s1:=Format(Fmt,[10]);   //  <---  compiler complains here
 
Fmt:='[%10.4e]';
s2:=Format(Fmt,[1.234]);
writeln(resfile, s1, s2);

closefile(resfile); 
end;     
           

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: trouble with format
« Reply #1 on: March 07, 2013, 03:19:03 pm »
Strange, working here (Linux, FPC 2.6.0 and 2.7.1)

Try this
1. Maybe it is namespace conflict (same Format function in other unit). Force it by:

s1 := SysUtils.Format(Fmt,[10]);

2. Maybe you have activated delphi mode in your unit?

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: trouble with format
« Reply #2 on: March 07, 2013, 03:27:27 pm »
Thanks a lot, that did it.

It now prints the numbers with square brackets around the numbers, how can I remove the brackets?

Best regards

Albert

Dibo

  • Hero Member
  • *****
  • Posts: 1057
Re: trouble with format
« Reply #3 on: March 07, 2013, 03:32:23 pm »
Just remove them ;)

Fmt:='%10d';

They were added only for demonstration

acp693

  • Jr. Member
  • **
  • Posts: 73
Re: trouble with format
« Reply #4 on: March 07, 2013, 03:46:41 pm »
Thank you, my brain doesn't work today... I was removing the square brackets from around the second argument in the format statement, and of course the compiler complained...

Thank you

Albert

 

TinyPortal © 2005-2018