placing output of printf in memory: formatting strings 
Author Message
 placing output of printf in memory: formatting strings

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




Sun, 05 Dec 1993 20:36:15 GMT  
 placing output of printf in memory: formatting strings

Quote:
>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|



Sun, 05 Dec 1993 22:31:57 GMT  
 placing output of printf in memory: formatting strings

Quote:
>    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

          ^^^
           |
           +--->  Note! Username is session, NOT sessions. Not my fault!
                                        Ask my SysAdmin why!!



Mon, 06 Dec 1993 04:04:15 GMT  
 placing output of printf in memory: formatting strings

Quote:
>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



Mon, 06 Dec 1993 07:37:52 GMT  
 placing output of printf in memory: formatting strings

Quote:

>    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).

Quote:
>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.

--



Tue, 07 Dec 1993 07:05:50 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. formatting output with printf, sprintf

2. How To Format Output With PRINTF

3. Enhanced printf which does output formatting like 100,000?

4. Format string as a parameter to printf()?

5. printf format string: how bad style is this?

6. printf -*s format string

7. Examples of format strings for scanf/printf ?

8. printf and formatting numeric strings

9. printf and variable length string format (asterisk)

10. Printf format strings

11. printf() format-string parser wanted!

12. printf Format string

 

 
Powered by phpBB® Forum Software