Hi,

I want to write a log function but I want to implement in the style of fprintf/sprintf style. ie

fprintf(arg1, template string, template values)
write_to_log(template string, some array of ints/double etc to fill the template)

I want to implement this variadic function:

void write_to_log(char template, int nargs, ...)
{
             printf(template, ???);
}

int main()
{
            write_to_log("client speed: %d\n", 1, 5);
}

I am stuck at line 3 where I want to do pass to printf whatever variable array of argument is passed to write_to_log.

What should I pass to printf inside write_to_log?

Besides
a) how to handle a mix of different types ie int / float etc just like printf?
b) how to handle string?

Thanks.

I did something like this:

#include <stdio.h>
#include <stdarg.h>

void write_to_log(char template[], int nargs, ...)
{
	va_list ap;
	va_start(ap, nargs);
	printf(template,ap);
	va_end(ap);
}

int main()
{
	write_to_log("client speed: %d\n",1,5);
	return 0;
}

getting compilation error:

test-var.c:4: error: expected ‘,’ or ‘...’ before ‘template’
test-var.c: In function ‘void write_to_log(char)’:
test-var.c:7: error: ‘nargs’ was not declared in this scope
test-var.c:8: error: expected primary-expression before ‘template’
test-var.c: In function ‘int main()’:
test-var.c:14: error: invalid conversion from ‘const char*’ to ‘char’
test-var.c:4: error: too many arguments to function ‘void write_to_log(char)’
test-var.c:14: error: at this point in file

Hi,

I want to write a log function but I want to implement in the style of fprintf/sprintf style. ie

fprintf(arg1, template string, template values)
write_to_log(template string, some array of ints/double etc to fill the template)

I want to implement this variadic function:

void write_to_log(char template, int nargs, ...)
{
             printf(template, ???);
}

int main()
{
            write_to_log("client speed: %d\n", 1, 5);
}

I am stuck at line 3 where I want to do pass to printf whatever variable array of argument is passed to write_to_log.

What should I pass to printf inside write_to_log?

Besides
a) how to handle a mix of different types ie int / float etc just like printf?
b) how to handle string?

Thanks.

shouldn't this:

void write_to_log(char template, int nargs, ...)

be:

void write_to_log(const char *template, int nargs, ...)

Plus here's a good example:

http://www.gnu.org/s/libc/manual/html_node/Variadic-Functions.html

shouldn't this:

void write_to_log(char template, int nargs, ...)

be:

void write_to_log(const char *template, int nargs, ...)

Plus here's a good example:

http://www.gnu.org/s/libc/manual/html_node/Variadic-Functions.html

I did what u suggest but still getting compilation error:

test-var.c:4: error: expected ‘,’ or ‘...’ before ‘template’
test-var.c: In function ‘void write_to_log(const char*)’:
test-var.c:7: error: ‘nargs’ was not declared in this scope
test-var.c:8: error: expected primary-expression before ‘template’
test-var.c: In function ‘int main()’:
test-var.c:4: error: too many arguments to function ‘void write_to_log(const char*)’
test-var.c:14: error: at this point in file

Besides once variable list is passed to write_to_log, how to hand it down to printf? I actually tried to follow this example which is copied into wikipedia (as you can see from my code snippet in my previous post.).

I compiled this and it worked fine

#include <stdio.h>
#include <stdarg.h>

void write_to_log(const char *template, int nargs, ...)
{
	int val = 0, val2 = 0;
	va_list ap;
	va_start(ap, nargs);
	val = va_arg (ap, int);
	val2 = va_arg(ap, int);	
	printf(template,val,val2);
	va_end(ap);
}

int main()
{
	write_to_log("client speed: %d, %d\n",1, 5, 8);
	return 0;
}

Program output - client speed: 5 8

Please look at the link I attach earlier

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.