Amiga.org
Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started 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....
-
What about sprintf ?
sprintf (string,format,data);
Bye,
Thomas
-
@Thomas
Sounds great :-)
-
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:
#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) "Fish";
array[1] = 2;
RawDoFmt("%s have %ld eyes.", array, (void (*)(void)) &stuffChar, string);
PutStr(string);
PutStr("\n");
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.
-
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.
-
@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().
-
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).
static const
UWORD stuffChar[] = {0x16c0, 0x4e75};
Is really:
_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.
-
Ahhh... ok. :-)