• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

c/c++ Arugment List

zappa86

Limp Gawd
Joined
Jul 1, 2005
Messages
162
How can I creating a function like printf that cant take unlimited arguments. I want to make a functing called Logger_Print and have it do what printf would basically do and more. I know you need to do something with the ... inorder to make it take many arguments.

Code:
void Logger_Print(int Type, char *Text, ... /*Hopefully a list of arguments */) {
	switch(Type) {
		case 0:
			fprintf(Logger_OutFile, "Verbose: ");
			break;
		case 1:
			fprintf(Logger_OutFile, "Status: ");
			break;
		case 2:
			fprintf(Logger_OutFile, "Notice: ");
			break;
	}

	fprintf(Logger_OutFile, Text, /* The list of arguments passed to this function */);
}

Please let me know if you know how to add the argument list for the Logger_Print to the fprintf call in the function. Thanks.
 
zappa86 said:
How can I creating a function like printf that cant take unlimited arguments.

Here's a good place to read up on variable argument lists. Here's a quick example I wrote showing how to use them:
Code:
#include <stdio.h>
#include <stdarg.h>

void print(int n, ...);

int main() {
    print(10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
}

void print(int n, ...) {
    int i;
    va_list va;
    
    va_start(va, n);
    for (i = 0; i < n; ++i)
        printf("%d ", va_arg(va, int));
    
    va_end(va);
}

Please let me know if you know how to add the argument list for the Logger_Print to the fprintf call in the function. Thanks.
Honestly, I don't know if it is possible to carry out everything in Logger_Print() using your strategy. For you see, va_arg() must be called every time you want to fetch an argument from the variable argument list, and printf() itself must be passed all of the variables at once.
 
just use vfprintf. I used vprintf here because I didn't want to cook up a file handle for an example:

Code:
void Logger_Print(int Type, char *Text, ... /*Hopefully a list of arguments */) {
	switch(Type) {
		case 0:
			printf( "Verbose: ");
			break;
		case 1:
			printf("Status: ");
			break;
		case 2:
			printf("Notice: ");
			break;
	}

	va_list valist;
	va_start(valist, Text);
	vprintf(Text, valist);
}

int main(int argc, char* argv[])
{
	Logger_Print(0, "%d, %d", 3, 5);
	return 0;
}
 
Back
Top