www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/07/21/05:34:09

Message-Id: <199807210930.GAA07054@swan2.uspnet.usp.br>
Comments: Authenticated sender is <hgfernan AT spider DOT uspnet DOT usp DOT br>
From: "Hilton Fernandes" <hgfernan AT usp DOT br>
To: djgpp mailing list <djgpp AT delorie DOT com>,
"Sean Middledich" <sean DOT middleditch AT use DOT net>
Date: Tue, 21 Jul 1998 06:29:40 +0000
MIME-Version: 1.0
Subject: Re: djgpp daily digest for 20 Jul 1998 2/2
Reply-to: Hilton Fernandes <hgfernan AT usp DOT br>
CC: "Hilton Fernandes" <hgfernan AT usp DOT br>
In-reply-to: <199807210406.AAA22718@delorie.com>

On 19 Jul 1998 15:02:36, "Sean Middledich" <sean DOT middleditch AT use DOT net> 
wrote:

> I was wondering how to the ... to make a printf type function.  My current
> project uses a screen buffer, but the function I use to write strings to the
> buffer can only use one string at a time.  If I wanted to do a porintf type
> effect, I have to do this:
> 
> char Text[81];
> 
> sprintf ( Text, "String A: %s, Number A: %d", StringA, NumB );
> Print ( Text );
> 
> I would love to take out the business with Text[81] and sprintf.  I just
> need to know how to use the ... ( elipse was it called? ).
> 
> Sean Middleditch ( the baffled programmer )
>  of
> AwesomePlay Productions
> http://www.angelfire.com/biz/aweplay
> aweplay AT hotmail DOT com
> 

Hi!

Please take a look at this very simple fprintff (fprintf with a 
flush):

#include <stdarg.h> /* va_list, va_start(), va_end */

int fprintff ( FILE* out_f, const char* format, ... )
{
        int     ret_val;
		va_list arg;


        va_start(arg, format);
        ret_val = vfprintf(out_f, format, arg);
        va_end(arg);

        if (fflush (out_f) == EOF)
        {
                return (EOF);
        }

        return (ret_val);
} /* int fprintff (FILE*, char*, ...) */


All you gotta do to make it become what you want is to change
vfprintf() to vsprintf().  And remove fflush(), of course. In the
example above, vfprintf() does the real magic, following the
argument list; va_start() is a macro that just makes arg point to the
next argument after format.  If there's one, of course.  Last __but__
least, va_arg() is the do-nothing macro and generates the empty
text.


Regards,
--Hilton

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019