Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Jose on September 10, 2005, 12:05:55 PM

Title: printf output to string possible ?
Post by: Jose on September 10, 2005, 12:05:55 PM
Hi. Is there a way that one can have the output of printf() put into a string instead of stdout ? I know there's a function to change the output stream but it requires a file or handle to be passed IIRC...
I have done a function that does this but it's a bit limited on the types it uses, I thought that maybe printf() could be used for this also....
Title: Re: printf output to string possible ?
Post by: Thomas on September 10, 2005, 12:09:14 PM

What about sprintf ?

sprintf (string,format,data);

Bye,
Thomas
Title: Re: printf output to string possible ?
Post by: Jose on September 10, 2005, 12:28:53 PM
@Thomas

Sounds great :-)
Title: Re: printf output to string possible ?
Post by: Piru on September 10, 2005, 12:44:32 PM
Indeed sprintf is your friend. However sometimes you want to avoid linking whole stdio just for that. You might then want to use exec/RawDoFmt:

Code: [Select]

#include <proto/exec.h>
#include <proto/dos.h>

static const
UWORD stuffChar[] = {0x16c0, 0x4e75};

int main(void)
{
  UBYTE string[256];
  LONG array[2];

  array[0] = (LONG) &quot;Fish&quot;;
  array[1] = 2;

  RawDoFmt(&quot;%s have %ld eyes.&quot;, array, (void (*)(void)) &stuffChar, string);

  PutStr(string);
  PutStr(&quot;\n&quot;);

  return 0;
}


However, please note that the default width of the %d, %u, %x and %c is 16-bit, not 32-bit as with printf functions. So you must use %ld, %lu, %lx and %lc if your array is LONGs. RawDoFmt is also limited in some other ways compared to sprintf. For simple stuff RawDoFmt is fine, however.
Title: Re: printf output to string possible ?
Post by: CrazyProg on September 10, 2005, 01:18:36 PM
Hi,

Instead of using 'sprintf' it is better to use 'snprintf'.  You specify how big the 'string' is so there isn't any chance of a buffer overrun crashing your program. :-)

If you are worried abount code size you could use the version in the newlib library.

Regards Neil.
Title: Re: printf output to string possible ?
Post by: Jose on September 10, 2005, 02:49:35 PM
@Piru
"(void (*)(void))"

Wouldn't a simple cast to void work here? I don't get it, it's not even a function.. :-?

@CrazyProg

Yeah that's cool, but I'm allocating the space dynamically after checking the size with strlen().

Title: Re: printf output to string possible ?
Post by: Piru on September 10, 2005, 03:05:35 PM
Quote
Wouldn't a simple cast to void work here? I don't get it, it's not even a function..

It isn't a function, but I want the compiler to believe it is (to avoid some warning).

Code: [Select]
static const
UWORD stuffChar[] = {0x16c0, 0x4e75};

Is really:
Code: [Select]

_stuffChar:
  move.b d0,(a3)+
  rts

It's much easier to use direct hexdump of the instructions than to try to get the compiler to produce something as simple.
Title: Re: printf output to string possible ?
Post by: Jose on September 11, 2005, 11:05:34 AM
Ahhh... ok.  :-)