Ebata Takayuki <hikaru _at_ mrh.biglobe.ne.jp> writes:
> >> char buff[1024];
> >> strcpy(buff, "abcdefg\n");
> >> vfprintf(stdout, "Result is %s\n", buff);
> 何をやりたいかいまいち不明ですが、ソースを見る限りはこの場合
> vfprintf()ではなく、fprintf()を使うべきかと…。
>
> ちなみに、vfprintf()の宣言は以下のようになっています。
> int vfprintf( FILE *stream, const char *format, va_list argptr );
> これによると、HONKYOUさんの使い方では、引数が足りないやうな…。
引数が足りないのでなくて、引数の型が違います。
vprintf, vfprintf, vsprintf は自前の可変引数な関数で良く使います。
例えば、メッセージ以外に、verbose が 0 以外の時に経過時間も出力する例:
#include <stdio.h>
#include <stdarg.h>
#include <sys/time.h>
#include <unistd.h>
static int callcount = 0;
int myprint(int verbose, struct timeval *start, char *fmt, ...)
{
int rc;
va_list ap;
callcount++;
if (verbose) {
struct timeval current;
if (!gettimeofday(¤t, NULL)) {
long ds = current.tv_sec - start->tv_sec;
long dm = current.tv_usec - start->tv_usec;
if (dm < 0) { ds--; dm += 1000*1000; }
(void)printf("%d: %d.%03d: ", callcount, ds, dm/1000);
}
}
va_start(ap, fmt);
rc = vfprintf(stdout, fmt, ap);
va_end(ap);
return rc;
}
int main(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) < 0) { perror("main:gettimeofday"); return 1; }
if (myprint(0, &tv, "abc\n") < 0) { return 1; }
(void)sleep(1);
if (myprint(1, &tv, "def\n") < 0) { return 1; }
return 0;
}
---------------------------------------------------------------------
tesigana _at_ mtf.biglobe.ne.jp
References:
- [fol] vfprintf.c no such file or directoryHONKYOU Tadashi
- [fol] Re: vfprintf.c no such file or directoryEbata Takayuki
- Prev by Date: [fol] Re: vfprintf.c no such file or directory
- Next by Date: [fol] printing system "No spool file found"
- Previous by thread: [fol] Re: vfprintf.c no such file or directory
- Next by thread: [fol] Re: vfprintf.c no such file or directory
- Indexes:[Main][Thread]