Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

printf, sprintf, fprintf functions for delphi

931 views
Skip to first unread message

ZSÉDENYI Zoltán

unread,
Sep 29, 2000, 3:00:00 AM9/29/00
to
Anyone heard any information about these functions?
i desperately looking for, thanks for any help or information!

Zoltan


Unknown

unread,
Sep 29, 2000, 3:00:00 AM9/29/00
to

Zoltan, look up Format function.


HTH
ain

ZSÉDENYI Zoltán

unread,
Sep 29, 2000, 3:00:00 AM9/29/00
to
Thank you, i know the format function, which is only sprintf lite.

Zoltan

<Ain Valtin> wrote in message news:39d49fae...@forums.inprise.com...

woody

unread,
Sep 29, 2000, 3:00:00 AM9/29/00
to
"ZSÉDENYI Zoltán" <zsz...@nepszabadsag.hu> wrote in message
news:8r2asb$2l...@bornews.borland.com...

> Thank you, i know the format function, which is only sprintf lite.
>
> Zoltan
>

Actually, it is all of them combined. You can use integers, floats, strings,
etc. all in the same formatting statement.

Woody

Peter Below (TeamB)

unread,
Sep 30, 2000, 3:00:00 AM9/30/00
to
In article <8r2200$rd...@bornews.borland.com>, ZSÉDENYI Zoltán wrote:
> Anyone heard any information about these functions?
> i desperately looking for, thanks for any help or information!
>
Zoltan,
what does printf or fprintf do that a simple WriteLn would not be able to
do?

Peter Below (TeamB) 10011...@compuserve.com)
No e-mail responses, please, unless explicitly requested!


ZSÉDENYI Zoltán

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
sprintf(c, "%04d",112); // will produce 0112
c:=format("%04d",112); // can't...

Thanks
Zoltan

"woody" <wood...@ih2000.net> wrote in message news:39d515eb_2@dnews...

ZSÉDENYI Zoltán

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
Peter,

WriteLn can not write strings! The only way to produce strings - like the C
sprintf - is the delphi's Format() function!
Especially, i don't need printf or fprintf, i have to use sprintf! Because,
format cannot replacing exactly the C sprintf function!
please tell me, how can is use the wvsprintf or wsprintf functions???
For example sprintf(c,"%04d",123) will producing 0123 while the
c:=format("%04d",123) produce ' 123' instead '0123';
now, i translating these strings. to make exact result, i need to change
'%04d' to'%.4d', and the format function can't parsing '\n' also, which is
also useful with MessageBox type functions.
My problem is coming from my partner, with an external language DLL with
prestored "C" strings type formatted messages... i NEED to use this DLL
messages exactly from my sw. Now i replacing format specifiers, and the \r
and \n translated to ^M


Thank you,
Zoltan


"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message
news:VA.00005e1...@antispam.compuserve.com...

Rudy Velthuis

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
ZSÉDENYI Zoltán wrote...

>sprintf(c, "%04d",112); // will produce 0112
>c:=format("%04d",112); // can't...

Of course! The formats are different (it's a different language after
all). So please read the help on format strings.

Try

C := Format('%.4d', [112]); // will produce 0112

--
Rudy Velthuis (Team JEDI)
http://delphi-jedi.org

ZSÉDENYI Zoltán

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
possible solution? This should work??? Any idea?

unit MyCLib;

interface

uses
Windows, SysUtils;

function sprintf(var Output: PChar; FormatStr: PChar; const arglist: array
of const): Integer;

implementation

function __wvsprintfA(Output: PChar; FormatStr: PChar; const arglist: array
of const): Integer; stdcall; external user32 name 'wvsprintfA';
function sprintf(var Output: PChar; FormatStr: PChar; const arglist: array
of const): Integer;
var
s: string;
i: Integer;
begin
Result:=__wvsprintfA( Output,FormatStr,arglist);
s:=Output;
s:=StringReplace(s,'\n',^M,[rfReplaceAll]);
s:=StringReplace(s,'\r',^M,[rfReplaceAll]);
s:=StringReplace(s,'\t',#9,[rfReplaceAll]);
for i:=1 to Length(s) do
Output[i-1]:=s[i];
Output[Length(s)]:=#0;
end;
end.

Thanks for all,
Zoltan

"ZSÉDENYI Zoltán" <zsz...@nepszabadsag.hu> wrote in message

news:8rc54k$3r...@bornews.borland.com...

ZSÉDENYI Zoltán

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
Last version... (after i experimenting null teminated string functions)
----------------------------------8<----------------------------------------
-
unit MyCLib;

interface

uses
Windows, SysUtils;
function sprintf(var Output: PChar; FormatStr: PChar; const arglist: array
of const): Integer;

implementation

function __wvsprintfA(Output: PChar; FormatStr: PChar; const arglist: array
of const): Integer; stdcall; external user32 name 'wvsprintfA';
function sprintf(var Output: PChar; FormatStr: PChar; const arglist: array
of const): Integer;
var
s: string;
i: Integer;
begin
Result:=__wvsprintfA( Output,FormatStr,arglist);
s:=Output;
s:=StringReplace(s,'\n',^M,[rfReplaceAll]);
s:=StringReplace(s,'\r',^M,[rfReplaceAll]);
s:=StringReplace(s,'\t',#9,[rfReplaceAll]);

StrPCopy(Output,s);
end;
end.
---------------------------------->8----------------------------------------
-

"ZSÉDENYI Zoltán" <zsz...@nepszabadsag.hu> wrote in message

news:8rcjap$8j...@bornews.borland.com...

Peter Below (TeamB)

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
In article <8rc54k$3r...@bornews.borland.com>, ZSÉDENYI Zoltán wrote:
> WriteLn can not write strings!

Rubbish. It can write all kinds of strings and formatted values. Its
abilities are a bit more limited than format, though. Basically WriteLn lets
you specify the output field width and (for floating point values) field
width and precision. WriteLn( filevar, integervalue:5 ) would format the
value right-aligned in a 5 character wide field, using blank padding. Its
described in the help under "Write procedure (for text files)".

It is easy to write your own based on format:

Procedure WriteLnFmt( Var F: Textfile; const formatstring: String;
const values: Array of const );
Begin
WriteLn(F, Format( formarstring, values ));
End;



> The only way to produce strings - like the C
> sprintf - is the delphi's Format() function!
> Especially, i don't need printf or fprintf, i have to use sprintf! Because,
> format cannot replacing exactly the C sprintf function!

It can replace it in all cases i can think of, the syntax is not the same, of
course, since Pascal is not C.

> please tell me, how can is use the wvsprintf or wsprintf functions???

You can use them if you feel masochistic.

wvsprintf expects a pointer to an array
of 4 byte values as last parameter. Don't ask my why Borland defined
va_list as an alias to Pchar, it will never be a true pchar for this
function. In case of a %s parameter wvsprintf expects to find a pchar in
the matching slot of the array, for a single parameter this would amount
to passing the address of the pchar, not the pchar (pointer) itself.
Example:

procedure TForm1.Button1Click(Sender: TObject);
var
oBuf: pChar;
valbuf: array [0..1] of longint;
S: String;
begin
S:= 'A';
valbuf[0] := longint( Pchar(s));
valbuf[1] := 1234;
oBuf := allocMem(20);
wvsprintf(oBuf, 'S: %s, n: %i', @valbuf );
MessageBox(handle, oBuf, '', mb_OK);
FreeMem(oBuf, 20);
end;

Don't expect wvsprintf to handle C escapes like \n, though. It is the C
preprocessor that does that, not the function. Use #10 instead.

> For example sprintf(c,"%04d",123) will producing 0123 while the
> c:=format("%04d",123) produce ' 123' instead '0123';
> now, i translating these strings. to make exact result, i need to change
> '%04d' to'%.4d', and the format function can't parsing '\n' also, which is
> also useful with MessageBox type functions.

As said above, use #10 instead of \n, 'Some text'#10'New line';

> My problem is coming from my partner, with an external language DLL with
> prestored "C" strings type formatted messages... i NEED to use this DLL
> messages exactly from my sw.

The DLL has obviously been designed from a C-centric worldview. It would not
be usable directly with VB, for example. By the way, i would not expect these
stored strings to contain escapes, they will be in the C *source* but the
stored strings will contain the translated character codes since they have
been processed by the precompiler. Unless the source specifies them with
doubled backslashes, of course.

Peter Below (TeamB)

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
In article <8rc45g$3r...@bornews.borland.com>, ZSÉDENYI Zoltán wrote:
> sprintf(c, "%04d",112); // will produce 0112
> c:=format("%04d",112); // can't...

c:= format('%.4d', [112] );

will. You just have to use the correct syntax.

ZSÉDENYI Zoltán

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
Thank you for the prompt answer!
My solution is working with external DLL!!!

for example, now i using! for ex.

sprintf(d, RESOURCESRING20021, [pNameOfDancer]);
if MessageBox( Handle, d, MB_YESNO+MB_APPLMODAL)=ID_YES then
begin
....

Thank you again, Zoltan

"Peter Below (TeamB)" <10011...@compuXXserve.com> wrote in message

news:VA.00005e6...@antispam.compuserve.com...

Rudy Velthuis

unread,
Oct 3, 2000, 3:00:00 AM10/3/00
to
ZSÉDENYI Zoltán wrote...
>Peter,
>
>WriteLn can not write strings! The only way to produce strings - like the C

>sprintf - is the delphi's Format() function!

I must have missed that. And since I didn't know it (and probably my
compiler didn't either), it worked for me.

Try this:

program Test;

{$APPTYPE CONSOLE}

uses SysUtils;

begin
WriteLn('This is a string');

Write('Press return...');
ReadLn;
end.

What you probably mean is that WriteLn doesn't return strings. But
neither does printf().

AntiRAM

unread,
Oct 4, 2000, 3:00:00 AM10/4/00
to
Uff Rudy...

Probably you don't know the difference between files, and strings...
So...
WriteLn write files stdout or everything that you specify
sprintf builds a string

Good morning!
:)

PS.: printf <> sprintf, like read <> spread and writeln<>sprintf
"Rudy Velthuis" <rvel...@gmx.de>
news:8rdctu$cj...@bornews.borland.com...


> ZSÉDENYI Zoltán wrote...
> >Peter,
> >
> >WriteLn can not write strings! The only way to produce strings - like the
C

write means build (possible)

> >sprintf - is the delphi's Format() function!

SPRINTF!!! and FORMAT!!! not printf and writeln!!!

>
> I must have missed that. And since I didn't know it (and probably my
> compiler didn't either), it worked for me.
>
> Try this:
>
> program Test;
>
> {$APPTYPE CONSOLE}
>
> uses SysUtils;
>
> begin
> WriteLn('This is a string');
>
> Write('Press return...');
> ReadLn;
> end.

Ooops.... This is a translated "Hello World" sample form Carnigan's First
steps with C? :)
hard class sample :)

>
> What you probably mean is that WriteLn doesn't return strings. But
> neither does printf().

Yes... WriteLn doesn't returns string. But neither does printf
Yes... Format returns string. But neither does sprintf

> --
> Rudy Velthuis (Team JEDI)
> http://delphi-jedi.org

AntiRAM (Team ObiVan)
http://delphi-kenobi.org


Rudy Velthuis

unread,
Oct 4, 2000, 3:00:00 AM10/4/00
to
AntiRAM wrote...

>Probably you don't know the difference between files, and strings...

Please read carefully to what I replied, and then come again:

>ZSÉDENYI Zoltán wrote...
>>Peter,
>>
>>WriteLn can not write strings!

Of course it can write them. It writes them to stdout. Just like it
writes many other kind of value too. It is called *write*ln after all.

It seems he wanted a function that produces strings. Writeln can't do
that (actually, it can, but not very easily - you'd have to redirect
stdout via the functions in the TTextRec for that file). Butthat was not
obvious from that message.

I know what sprintf, printf and fprintf do. I even know that there is no
spread (but a sscanf, fscanf and scanf). And I know that in OP, Format is
the only way to produce strings similar to sprintf. If he really needs to
use the exact same strings as sprintf, he'll have to use a C function,
like the wvsprintf in the API.

Rudy Velthuis

unread,
Oct 4, 2000, 3:00:00 AM10/4/00
to
AntiRAM wrote...

>Probably you don't know the difference between files, and strings...

See my other message - it's sent already, so I posted twice. If you think
that I don'Ät know the difference between a file and a string, then it's
useless to discuss this.

>write means build (possible)

Huh? I don't understand that


>> >sprintf - is the delphi's Format() function!
>
>SPRINTF!!! and FORMAT!!! not printf and writeln!!!

Please see the subject line. He mentions printf, sprintf and fprintf
explicitly. I was not the one that mentioned Writeln, but traditionally,
Writeln is the function in Pascal, that did what printf in C did. It only
became clear later on, that the original poster was only interested in a
function that accepts C-style format specifiers.

In his message, it looked like he meant that Writeln can't process
stirngs, unless you use the Format function. See the replies of others,
who assumed the same from his message.

>Ooops.... This is a translated "Hello World" sample form Carnigan's First
>steps with C? :)
>hard class sample :)

That was a way to demonstrate that Writeln can write strings. It can't
write TO strings, however. There is a significant difference.

>Yes... WriteLn doesn't returns string. But neither does printf
>Yes... Format returns string. But neither does sprintf

So? See above.

woody

unread,
Oct 4, 2000, 3:00:00 AM10/4/00
to
"ZSÉDENYI Zoltán" <zsz...@nepszabadsag.hu> wrote in message
news:8rc45g$3r...@bornews.borland.com...

> sprintf(c, "%04d",112); // will produce 0112
> c:=format("%04d",112); // can't...
>
> Thanks
> Zoltan
As Rudy and Peter have pointed out, it's a matter of removing the "C"
thinking from your brain and replacing it with "Pascal" thinking. There are
different ways to do the same thing in almost all languages. You just can't
expect them to all have the same syntax. <g>

Woody

Colin Wilson

unread,
Oct 9, 2000, 3:00:00 AM10/9/00
to
BTW, Here's how you can use WriteLn to write to strings...

1. Declare a TTextStr class (see below).

2. Use it like this...

procedure TfmMain.Button1Click(Sender: TObject);
var
st : TTextStr;
begin
st := TTextStr.Create;
try
WriteLn (st.Handle, 'The value of PI is ', pi:2:8);
Label1.Caption := st.Text;

WriteLn (st.Handle, 'The quick brown fox jumps over ', 99, ' lazy
dogs');
Label2.Caption := st.Text;
finally
end
end;

...........


and here's what a TTextStr class looks like. The trouble is that the
TeamB people pretend that they never heard of Turbo Pascal!

unit unitTextStr;

interface

uses Windows, Classes, SysUtils;

type
TTextStr = class
private
buffer : string;
f : system.text;
function GetText: string;

public
constructor Create;
property Text : string read GetText;
property Handle : system.text read f;
end;

implementation

const
CHUNK_SIZE = 256;

type
PTextRec = ^TTextRec;

{ TTextStr }

function NilIOFunc (var f : text) : Integer;
begin
result := 0
end;

function OutStr (var f : text) : Integer;
var
t : PTextRec;
st : TTextStr;
begin
t := PTextRec (@f);
Move (t^.UserData, st, sizeof (st));
SetLength (st.buffer, CHUNK_SIZE + t^.BufSize);
Inc (t^.BufSize, CHUNK_SIZE);
t^.BufPtr := @st.buffer [1];
result := 0;
end;

constructor TTextStr.Create;
var
t : PTextRec;
begin
t := PTextRec (@f);
Move (self, t^.UserData, sizeof (self));
t^.Mode := fmOutput;
t^.OpenFunc := @NilIOFunc;
t^.CloseFunc := @NilIOFunc;
t^.FlushFunc := @NilIOFunc;
t^.InOutFunc := @OutStr;
t^.BufSize := CHUNK_SIZE;
SetLength (buffer, t^.BufSize);
t^.BufPtr := @buffer [1];
end;

function TTextStr.GetText: string;
var
t : PTextRec;
begin
t := PTextRec (@f);
result := buffer;
t^.BufPos := 0;
t.BufEnd := 0
end;

end.


Colin
e-mail :co...@wilsonc.demon.co.uk
web: http://www.wilsonc.demon.co.uk/delphi.htm


Rudy Velthuis

unread,
Oct 9, 2000, 3:00:00 AM10/9/00
to
Colin Wilson wrote...

>and here's what a TTextStr class looks like. The trouble is that the
>TeamB people pretend that they never heard of Turbo Pascal!

Ah, but I mentioned you could, by redirecting the output via the
functions in the TextRec. I have written similar code before, years and
years ago.

0 new messages