gtpc2m9fC/C++ Language Support User's Guide

vsprintf-Format and Print Data to a Buffer

This function formats and writes data to a buffer.

Format

#include <stdarg.h>
#include <stdio.h>
int vsprintf(char *target-string, const char *format., va_listst arg_ptr);

target-string
The buffer to which formatted output is to be written.

format
The format template.

arg_ptr
The replacement arguments for format.

The vsprintf function is similar to the sprintf function except that arg_ptr points to a list of arguments whose number can vary from call to call in the program. In contrast, the sprintf function can have a list of arguments, but the number of arguments in that list is fixed when you compile the program. For a specification of the format string, see fprintf, printf, sprintf-Format and Write Data.

Normal Return

If there is no error, the vsprintf function returns the number of characters written to target-string.

Error Return

If an error occurs, the vsprintf function returns a negative value.

Note:
In contrast to some UNIX-based implementations of C language, the TPF C library implementation of the vprintf family increments the pointer to the variable arguments list. To control whether the pointer to the argument is incremented, call the va_end macro after each call to the vsprintf function.

Programming Considerations

None.

Examples

The following example assigns a variable number of strings to string and prints the string that is the result using the vsprintf function.

#include <stdarg.h>
#include <stdio.h>
 
void vout(char *string, char *fmt, ...);
char fmt1 [] = "%s  %s  %s\n";
 
int main(void)
{
   char string[100];
 
   vout(string, fmt1, "Sat", "Sun", "Mon");
   printf("The string is:  %s\n", string);
}
void vout(char *string, char *fmt, ...)
 
{
   va_list arg_ptr;
 
   va_start(arg_ptr, fmt);
   vsprintf(string, fmt, arg_ptr);
   va_end(arg_ptr);
}

Output

The string is:  Sat  Sun  Mon

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.