[comp.lang.c] placing output of printf in memory: formatting strings

luntz@acsu.buffalo.edu (jon e luntz) (06/19/91)

Hi,
	I'm working with a serial device on a machine where I need to use a
device driver to access the port (ie. I cannot just fopen the port).  The way
the driver works is it takes the output string straight from memory.  What I'd
like to do is to be able to write formatted output to memory, such as that from
a printf or fprintf.  Is there any way to fopen a string pointer or a memory 
location, or maybe some way to redirect output to memory temporarily?  I'd
appreciate any advice.
		Thanks,
		Jon Luntz
		Internet:	luntz@mars.epm.ornl.gov
			    or  luntz@acsu.buffalo.edu

jos@and.nl (J. Horsmeier) (06/19/91)

In article <80463@eerie.acsu.Buffalo.EDU> luntz@acsu.buffalo.edu (jon e luntz) writes:
>Hi,
>	I'm working with a serial device on a machine where I need to use a
>device driver to access the port (ie. I cannot just fopen the port).  The way
>the driver works is it takes the output string straight from memory.  What I'd
>like to do is to be able to write formatted output to memory, such as that from
>a printf or fprintf.  Is there any way to fopen a string pointer or a memory 
>location, or maybe some way to redirect output to memory temporarily?  I'd
>appreciate any advice.
>		Thanks,
>		Jon Luntz

Hi there, 

sprintf(3V) would do the job for you.

Jos

|O   J.A. Horsmeier AND Software B.V.        phone : +31 10 4367100   O|
|O                  Westersingel 106/108     fax   : +31 10 4367110   O|
|O                  3015 LD  Rotterdam NL    e-mail: jos@and.nl       O|

session@seq.uncwil.edu (Zack C. Sessions) (06/20/91)

luntz@acsu.buffalo.edu (jon e luntz) writes:

>	I'm working with a serial device on a machine where I need to use a
>device driver to access the port (ie. I cannot just fopen the port).  The way
>the driver works is it takes the output string straight from memory.  What I'd
>like to do is to be able to write formatted output to memory, such as that from
>a printf or fprintf.  Is there any way to fopen a string pointer or a memory 
>location, or maybe some way to redirect output to memory temporarily?  I'd
>appreciate any advice.

Use sprintf(). For example,

	int number;
	char string[80];
	sprintf(string,"%d",number);


Zack C. Sessions
session@seq.uncwil.edu
	  ^^^
	   |
	   +--->  Note! Username is session, NOT sessions. Not my fault!
					Ask my SysAdmin why!!

scs@adam.mit.edu (Steve Summit) (06/20/91)

In article <80463@eerie.acsu.Buffalo.EDU> luntz@acsu.buffalo.edu (jon e luntz) writes:
>What I'd
>like to do is to be able to write formatted output to memory, such as that from
>a printf or fprintf.  Is there any way to fopen a string pointer or a memory 
>location, or maybe some way to redirect output to memory temporarily?

Well, so far we've had two identical answers, but I'm not sure
they're what Mr. Luntz was looking for.  (I suspect he knows about
sprintf, although it is an obvious answer if you only read every
third word of the original question.)

What Mr. Luntz probably wants to do is something like

	extern FILE *stropen();
	char buf[80];
	FILE *sfp = stropen(buf, "w");
	fprintf(sfp, "Hello, ");
	fputs("world!", sfp);
	putc('\n', sfp);

which would leave the conglomerate string "Hello, world!\n" in buf.
Note that sfp is an apparently-ordinary FILE *, upon which any
sequence of stdio output operations can be performed, except that
the text simply accumulates in buf rather than being written to
some "file."

Note that if you have stropen (or something like it) and
vfprintf, you can implement sprintf in terms of it.

I'm not sure how Mr. Luntz plans to empty the buffer as its
characters are consumed by his "device driver."  More useful
would be a way to arrange for the characters "written" to a FILE *
to be neither written to a "file" nor accumulated in a string,
but rather passed to a user-defined output function.  Something
like:

	int mywrite(char *chars, int nchars)
	{
	}

	extern FILE *funopen();

	FILE *fp = funopen((int (*)())NULL, mywrite);

Either Chris Torek or I can supply you with stdio implementations
which let you do these sorts of things.  (funopen or its
equivalent actually takes a few more arguments, but I can't
remember how mine or Chris's works.)  Unfortunately, the extended
string and function I/O functions (i.e. stropen and funopen) are
nowhere near standard.

                                            Steve Summit
                                            scs@adam.mit.edu

datangua@watmath.waterloo.edu (David Tanguay) (06/21/91)

In article <1991Jun19.233752.24019@athena.mit.edu> scs@adam.mit.edu writes:
>	extern FILE *stropen();
>	char buf[80];
>	FILE *sfp = stropen(buf, "w");
>	fprintf(sfp, "Hello, ");
>	fputs("world!", sfp);
>	putc('\n', sfp);

Our compiler implements this functionality via
	sfp = fopen(buf, "ws"); /* note the "s" for string */
There are corrsponding "rs", "as", "wb", etc. modes for reading strings
or raw bytes from memory (the "b"). There should be a way of specifying
the maximum string length: fopen(buf, "ws:80") or stropen(buf, "w", 80).

It's in our run-time because the C run-time is really just an interface
to the B run-time (although we have since propagated the functionality).

>More useful
>would be a way to arrange for the characters "written" to a FILE *
>to be neither written to a "file" nor accumulated in a string,
>but rather passed to a user-defined output function.
-- 
David Tanguay        datanguay@watmath.waterloo.edu        Thinkage, Ltd.