vsnprintf{} 将可变参数格式化输出到一个字符数组
- 1. function `vsnprintf()`
- 1.1. `const int num_bytes = vsnprintf(NULL, 0, format, arg);`
- 2. Parameters
- 3. Return value
- 4. Example
- 5. llama.cpp
- References
1. function vsnprintf()
https://cplusplus.com/reference/cstdio/vsnprintf/
int vsnprintf(char *target_str, size_t n, const char *format, va_list arg);
Write formatted data from variable argument list to sized buffer
Composes a string with the same text that would be printed if format
was used on printf
, but using the elements in the variable argument list identified by arg
instead of additional function arguments and storing the resulting content as a C string in the buffer pointed by target_str
(taking n
as the maximum buffer capacity to fill).
使用在 printf
上使用 format
时打印的相同文本组成一个字符串,但使用 arg
标识的变量参数列表中的元素而不是额外的函数参数,并将结果内容作为 C 字符串存储在 target_str
指向的缓冲区中 (以 n
作为要填充的最大缓冲区容量)。
If the resulting string would be longer than n - 1
characters, the remaining characters are discarded and not stored, but counted for the value returned by the function.
如果结果字符串长度超过 n - 1
个字符,则剩余字符将被丢弃并且不被存储,但计入函数返回的值。
#include <stdarg.h>
#include <stdio.h>
void formatted_data(char *str, char *fmt, ...);
int main(void) {
char str[256];
formatted_data(str, "%s-%s-%s\n", "Yong", "Qiang", "Cheng");
printf("The string: %s\n", str);
return 0;
}
void formatted_data(char *str, char *fmt, ...) {
va_list arg_ptr;
va_start(arg_ptr, fmt);
const int num_bytes = vsnprintf(str, 10, fmt, arg_ptr);
va_end(arg_ptr);
printf("The number of bytes: %d\n", num_bytes);
}
The number of bytes: 17
The string: Yong-Qian
请按任意键继续. . .
Internally, the function retrieves arguments from the list identified by arg
as if va_arg()
was used on it, and thus the state of arg
is likely to be altered by the call.
在内部,该函数从 arg
标识的列表中检索参数,就好像在其上使用 va_arg()
一样,因此 arg
的状态可能会因调用而改变。
In any case, arg
should have been initialized by va_start()
at some point before the call, and it is expected to be released by va_end()
at some point after the call.
无论如何,arg
应该在调用之前的某个时间点由 va_start()
初始化,并且预计在调用之后的某个时间点由 va_end()
释放。
1.1. const int num_bytes = vsnprintf(NULL, 0, format, arg);
int vsnprintf(char *target_str, size_t n, const char *format, va_list arg);
const int num_bytes = vsnprintf(NULL, 0, format, arg);
At most n - 1
characters are written. The resulting character string will be terminated with a null character, unless n
is zero.
最多写入 n - 1
个字符。除非 n
为零,否则生成的字符串将以空字符 (\0
) 结尾。
If n
is zero, nothing is written and target_str
may be a null pointer, however the return value (number of bytes that would be written not including the null terminator) is still calculated and returned.
如果 n
为零,则不会写入任何内容,并且 target_str
可能为空指针,但是仍会计算并返回值 (不包括空终止符的写入字节数)。
#include <iostream>
#include <cstdarg>
#include <vector>
std::string string_format(const char *format, ...) {
va_list ap1;
va_list ap2;
va_start(ap1, format);
va_copy(ap2, ap1);
const int size1 = vsnprintf(NULL, 0, format, ap1);
std::vector<char> buf(size1 + 1);
const int size2 = vsnprintf(buf.data(), size1 + 1, format, ap2);
std::cout << "size1=" << size1 << ", size2=" << size2 << std::endl;
std::cout << "buf: " << buf.data() << std::endl;
va_end(ap2);
va_end(ap1);
return std::string(buf.data(), size1);
}
int main() {
const std::string batch = string_format("logical maximum batch size (default: %d)", 9);
std::cout << batch.c_str() << std::endl << std::endl;
const std::string ubatch = string_format("physical maximum batch size (default: %d)", 16);
std::cout << ubatch.c_str() << std::endl;
return 0;
}
2. Parameters
target_str
Pointer to a buffer where the resulting C-string is stored.
The buffer should have a size of at least n
characters.
n
Maximum number of bytes to be used in the buffer.
The generated string has a length of at most n - 1
, leaving space for the additional terminating null character.
生成的字符串的长度最多为 n - 1
,为额外的终止空字符留出空间。vsnprintf()
要在结果的末尾追加 \0
。
size_t
is an unsigned integral type.
format
C string that contains a format string that follows the same specifications as format
in printf
(see printf
for details).
arg
A value identifying a variable arguments list initialized with va_start()
.
va_list
is a special type defined in <cstdarg>
.
3. Return value
The vsnprintf()
function returns the number of bytes that are written in the array, not counting the ending null character.
vsnprintf()
函数返回写入数组的字节数,不计算结尾的空字符。
The number of characters that would have been written if n
had been sufficiently large, not counting the terminating null character
.
写入的字符数,不包括终止空字符。
If an encoding error occurs, a negative number is returned.
Notice that only when this returned value is non-negative and less than n
, the string has been completely written.
注意,只有当这个返回值非负且小于 n
时,字符串才被完全写入。
#include <stdarg.h>
#include <stdio.h>
void formatted_data(char *str, char *fmt, ...);
int main(void) {
char str[256];
formatted_data(str, "%s-%s-%s\n", "Yong", "Qiang", "Cheng");
printf("The string: %s\n", str);
return 0;
}
void formatted_data(char *str, char *fmt, ...) {
va_list arg_ptr;
va_start(arg_ptr, fmt);
const int num_bytes = vsnprintf(str, 64, fmt, arg_ptr);
va_end(arg_ptr);
printf("The number of bytes: %d\n", num_bytes);
}
The number of bytes: 17
The string: Yong-Qiang-Cheng
请按任意键继续. . .
The number of characters written if successful or negative value if an error occurred. If the resulting string gets truncated due to n
limit, function returns the total number of characters (not including the terminating null-byte) which would have been written, if the limit was not imposed.
如果成功则返回写入的字符数,如果发生错误则返回负值。如果结果字符串由于 n
限制而被截断,则函数返回在未施加限制的情况下应写入的总字符数 (不包括终止空字节)。
- 如果没有截断,则返回成功打印到
target_str
中的字符个数,字符个数不包括末尾追加的\0
。 - 如果发生截断,则返回不限制打印字符数量的情况下可以写入的字符个数,字符个数不包括末尾追加的
\0
。 - 如果格式化解析失败,则返回负数。
4. Example
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdarg.h>
void PrintError(const char *format, ...) {
char buffer[256];
va_list args;
va_start(args, format);
const int num_bytes = vsnprintf(buffer, 256, format, args);
printf("The number of bytes: %d\n", num_bytes);
perror(buffer);
va_end(args);
}
int main() {
char FileName[] = "qiang.txt";
FILE *file_ptr = fopen(FileName, "r");
if (NULL == file_ptr) {
PrintError("Open %s", FileName);
}
else {
// file successfully open
fclose(file_ptr);
}
return 0;
}
5. llama.cpp
https://github.com/ggerganov/llama.cpp
/home/yongqiang/llm_work/llama_cpp_25_01_05/llama.cpp/src/llama-arch.cpp
#include <iostream>
#include <cstdarg>
#include <vector>
std::string format(const char *fmt, ...) {
va_list ap1;
va_list ap2;
va_start(ap1, fmt);
va_copy(ap2, ap1);
const int size1 = vsnprintf(NULL, 0, fmt, ap1);
std::vector<char> buf(size1 + 1);
const int size2 = vsnprintf(buf.data(), size1 + 1, fmt, ap2);
std::cout << "size1=" << size1 << ", size2=" << size2 << std::endl;
std::cout << "buf: " << buf.data() << std::endl;
va_end(ap2);
va_end(ap1);
return std::string(buf.data(), size1);
}
int main() {
const std::string output_norm = format("output_norm", 1, 2);
std::cout << output_norm.c_str() << std::endl << std::endl;
const std::string attn_q = format("blk.%d.attn_q", 3, 4);
std::cout << attn_q.c_str() << std::endl << std::endl;
const std::string ffn_down = format("blk.%d.ffn_down.%d", 5, 6);
std::cout << ffn_down.c_str() << std::endl << std::endl;
const std::string ffn_up = format("blk.%d.ffn_up.%d", 7, 8);
std::cout << ffn_up.c_str() << std::endl << std::endl;
return 0;
}
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/
[2] vsnprintf() - Print Argument Data to Buffer, https://www.ibm.com/docs/en/i/7.5?topic=functions-vsnprintf-print-argument-data-buffer